问题描述
请查看下面的代码:
// 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?
我们什么时候应该在BufferedWriter上使用PrintWriter?
When should we use PrintWriter over BufferedWriter?
推荐答案
和详细说明了这些差异。
The API reference for BufferedWriter and PrintWriter detail the differences.
使用PrintWriter的主要原因是可以访问printXn(如println()。您实际上可以使用PrintWriter写入文件,就像使用System.out写入控制台一样。
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.
BufferedWriter是一种写入a的有效方法。文件(或其他任何东西),因为它将缓冲Java内存中的字符(可能,取决于实现)下降到C以写入文件。
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.
没有PrintReader这样的概念;你得到的最接近的可能是。
There is no such concept as a "PrintReader"; the closest you will get is probably java.util.Scanner.
这篇关于java.io.PrintWriter和java.io.BufferedWriter之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!