Java 中的 PrintWriter 与 FileWriter
Java中的PrintWriter和FileWriter是否相同,无论使用哪一个?到目前为止,我已经使用了这两种方法,因为它们的结果是相同的。在某些特殊情况下,是否有必要选择其中一种而不是另一种?
public static void main(String[] args) {
File fpw = new File("printwriter.txt");
File fwp = new File("filewriter.txt");
try {
PrintWriter pw = new PrintWriter(fpw);
FileWriter fw = new FileWriter(fwp);
pw.write("printwriter text\r\n");
fw.write("filewriter text\r\n");
pw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据coderanch.com,如果我们结合我们得到的答案:
FileWriter是IO的字符表示。这意味着它可以用来书写字符。在内部,FileWriter 会使用底层操作系统的默认字符集,并将字符转换为字节并将其写入磁盘。
打印作家和文件编写器。
相似之处
差异
PrintStream 和 OutputStream 的区别:与上面的解释类似,只是将字符替换为字节。
PrintWriter 有以下方法:
和构造函数是:
而 FileWriter 有以下方法:
和构造函数是:
链接: http://www.coderanch.com/t/418148/java-programmer-SCJP/certification/Information-PrintWriter-FileWriter
According to coderanch.com, if we combine the answers we get:
FileWriter is the character representation of IO. That means it can be used to write characters. Internally FileWriter would use the default character set of the underlying OS and convert the characters to bytes and write it to the disk.
PrintWriter & FileWriter.
Similarities
Differences
Difference between PrintStream and OutputStream: Similar to the explanation above, just replace character with byte.
PrintWriter has following methods :
and constructors are :
while FileWriter having following methods :
and constructors are :
Link: http://www.coderanch.com/t/418148/java-programmer-SCJP/certification/Information-PrintWriter-FileWriter
这两者都在内部使用了 FileOutputStream:
但主要区别在于 PrintWriter 提供了特殊方法:
Both of these use a
FileOutputStream
internally:but the main difference is that PrintWriter offers special methods:
PrintWriter
有不同的错误处理概念。您需要调用checkError()
而不是使用 try/catch 块。A
PrintWriter
has a different concept of error handling. You need to callcheckError()
instead of using try/catch blocks.PrintWriter
不会抛出IOException
。为此,您应该调用checkError()
方法。PrintWriter
doesn't throwIOException
.You should callcheckError()
method for this purpose.Java5+ 中的 java.io.PrintWriter 允许使用方便的方法/构造函数写入文件。
来自 Javadoc;
使用指定的文件创建一个新的 PrintWriter,不自动行刷新。这个方便的构造函数创建必要的中间 OutputStreamWriter,它将使用 Java 虚拟机实例的默认字符集对字符进行编码。
The
java.io.PrintWriter
in Java5+ allowed for a convenience method/constructor that writes to file.From the Javadoc;
Creates a new PrintWriter, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.
只是为了提供与 FileOutputStreamlush() 相关的 FLUSH 和 Close 方法相关的更多信息
---只是确保任何缓冲的数据完全写入磁盘并准备好之后再次写入流(或写入器)。
close() ----刷新数据并关闭所有文件句柄、套接字或其他任何内容。现在连接已丢失,您无法向outputStream 写入任何内容。
just to provide more info related to FLUSH and Close menthod related to FileOutputStream
flush() ---just makes sure that any buffered data is written to disk flushed compltely and ready to write again to the stream (or writer) afterwards.
close() ----flushes the data and closes any file handles, sockets or whatever.Now connection has been lost and you can't write anything to outputStream.
事实上 java.io.FileWriter 依赖于平台的默认字符编码,这使得它对我来说毫无用处。您永远不应该对部署应用程序的环境做出任何假设。
The fact that java.io.FileWriter relies on the default character encoding of the platform makes it rather useless to me. You should never assume something about the environment where your app will be deployed.