java.io.PrintWriter 和 java.io.BufferedWriter 之间的区别?
请看下面的代码:
// A.class
File file = new File("blah.txt");
FileWriter fileWriter = new FileWriter(file);
PrintWriter printWriter = new PrintWriter(fileWriter);
// B.class
File file = new File("blah.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bWriter = new BufferedWriter(fileWriter);
这两种方法有什么区别?
我们什么时候应该使用 PrintWriter 而不是 BufferedWriter?
Please look through code below:
// A.class
File file = new File("blah.txt");
FileWriter fileWriter = new FileWriter(file);
PrintWriter printWriter = new PrintWriter(fileWriter);
// B.class
File file = new File("blah.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bWriter = new BufferedWriter(fileWriter);
What is the difference between these two methods?
When should we use PrintWriter over BufferedWriter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
PrintWriter
提供了更多方法 (println
),但需要注意的最重要(也是令人担忧)的区别是它会吞下异常。您可以稍后调用
checkError
来查看是否发生了任何错误,但通常您会使用PrintWriter
来执行诸如写入控制台之类的操作 - 或者在“quick 'n dirty”中“您不想被异常所困扰的应用程序(并且长期可靠性不是问题)。我不确定为什么“额外的格式化能力”和“不要吞下异常”方面被捆绑到同一个类中 - 格式化显然在许多您不希望出现异常的地方很有用被吞噬。很高兴看到 BufferedWriter 在某个时候获得相同的能力......
PrintWriter
gives more methods (println
), but the most important (and worrying) difference to be aware of is that it swallows exceptions.You can call
checkError
later on to see whether any errors have occurred, but typically you'd usePrintWriter
for things like writing to the console - or in "quick 'n dirty" apps where you don't want to be bothered by exceptions (and where long-term reliability isn't an issue).I'm not sure why the "extra formatting abilities" and "don't swallow exceptions" aspects are bundled into the same class - formatting is obviously useful in many places where you don't want exceptions to be swallowed. It would be nice to see
BufferedWriter
get the same abilities at some point...BufferedWriter 的 API 参考 和 PrintWriter 详细说明了差异。
使用 PrintWriter 的主要原因是访问 printXXX 方法,例如 println()。本质上,您可以使用 PrintWriter 写入文件,就像使用 System.out 写入控制台一样。
BufferedWriter 是写入文件(或其他任何内容)的有效方法,因为它会在(可能取决于实现)下降到 C 写入文件之前缓冲 Java 内存中的字符。
不存在“PrintReader”这样的概念;您将得到的最接近的可能是 java.util.Scanner。
The API reference for BufferedWriter and PrintWriter detail the differences.
The main reason to use the PrintWriter is to get access to the printXXX methods like println(). You can essentially use a PrintWriter to write to a file just like you would use System.out to write to the console.
A BufferedWriter is an efficient way to write to a file (or anything else), as it will buffer the characters in Java memory before (probably, depending on the implementation) dropping to C to do the writing to the file.
There is no such concept as a "PrintReader"; the closest you will get is probably java.util.Scanner.
正如TofuBeer的回答中所说,两者都有其特色。要充分利用 PrintWriter (或任何其他编写器)的优势,同时也使用缓冲写入,您可以使用所需的写入器包装 BufferedWriter,如下所示:
As said in TofuBeer's answer both have their specialties. To take the full advantage of PrintWriter (or any other writer) but also use buffered writing you can wrap the BufferedWriter with the needed one like this:
PrintWriter 只是在字符模式下公开任何 Writer 上的打印方法。
根据其缓冲方法,BufferedWriter 比 BufferedWriter 更高效。
根据您的系统平台,它附带一个 newLine() 方法来正确操作文本文件。
BufferedReader 允许从文件中读取文本,并将字节转换为字符。它允许逐行读取。
没有PrintReader,您必须根据输入的格式选择另一个Reader实现。
PrintWriter just exposes the print methods on any Writer in character mode.
BufferedWriter is more efficient than , according to its buffered methods.
And it comes with a newLine() method, depending of your system platform, to manipulate text files correctly.
The BufferedReader permits to read a text from file, with bytes converted in characters. It allows to read line by line.
There is no PrintReader, you have to choose another Reader implementation according to the format of your input.
PrintWriter是将字符数据写入文件的最增强的Writer。
PrintWriter 相对于 FileWriter 和 BufferedWriter 的主要优点是:
PrintWriter printwriter = new PrintWriter("blah.txt");
(或)
我们可以将任何类型的原始数据直接写入文件(因为我们有 PrintWriter 方法的附加重载版本,即, print() 和 println())。
printwriter.print(65); //65
bufferedwriter.write(65); //A
printwriter.println(true); //true
PrintWriter is the most enhanced Writer to write Character data to a file.
The main advantage of PrintWriter over FileWriter and BufferedWriter is:
PrintWriter printwriter = new PrintWriter("blah.txt");
(or)
We can write any type of Primitive data directly to the file (because we have additional overloaded versions of PrintWriter methods i.e., print() and println()).
printwriter.print(65); //65
bufferedwriter.write(65); //A
printwriter.println(true); //true
一般来说,Writer 会立即将其输出发送到底层字符或字节流。除非需要提示输出,否则建议将 BufferedWriter 包装在任何 write() 操作可能成本较高的 Writer 周围,例如 FileWriters 和 OutputStreamWriters。例如,
注意:代码块中的文本内容会自动换行
PrintWriter out =
new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
将把 PrintWriter 的输出缓冲到文件中。如果没有缓冲,每次调用 print() 方法都会导致字符转换为字节,然后立即写入文件,这可能非常低效。
In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,
Note: Text content in the code blocks is automatically word-wrapped
PrintWriter out =
new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.
BufferedWriter - 将文本写入输出字符流,缓冲字符流中的字符。
PrintWriter - 将对象的格式化表示打印到文本输出流。
BufferedWriter - Writes text to an output character stream, buffering characters from a character stream.
PrintWriter - Prints formatted representations of objects to a text output stream.
FileWriter 和 BufferedWriter 需要为每一行添加一个新的行分隔符,想象一下必须写入 100 行,而 PrintWriter 有 println() 方法可以做到这一点。最重要的是,FileWriter 和 BufferedWriter 只能写入 char 和 String 数据类型,而 PrintWriter 还可以写入 int、double 和 boolean 数据类型
FileWriter and BufferedWriter need an new line separator for each line, just imagine having to write 100 lines whereas PrintWriter has the println() method that does that. Most importantly, FileWriter and BufferedWriter can write char and String data types ONLY whereas PrintWriter can also write int, double, and boolean data types
我认为使用 PrintWriter 背后的原因已经在上面提到过,但重要的原因之一是您将文件对象直接传递给 PrintWriter 构造函数,这使得使用它变得很容易。
I think that the reason behind using PrintWriter is already mentioned above but one of the important reason is you an pass a file object directly to the PrintWriter constructor which makes it easy to use it.