java.io.PrintWriter 和 java.io.BufferedWriter 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1747040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between java.io.PrintWriter and java.io.BufferedWriter?
提问by i2ijeya
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?
我们什么时候应该使用 PrintWriter 而不是 BufferedWriter?
采纳答案by TofuBeer
The API reference for BufferedWriterand PrintWriterdetail the differences.
BufferedWriter和PrintWriter的 API 参考详细说明了差异。
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.
使用 PrintWriter 的主要原因是可以访问 printXXX 方法,如 println()。您基本上可以使用 PrintWriter 写入文件,就像使用 System.out 写入控制台一样。
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.
BufferedWriter 是一种写入文件(或其他任何东西)的有效方法,因为它会在(可能,取决于实现)丢弃到 C 来写入文件之前缓冲 Java 内存中的字符。
There is no such concept as a "PrintReader"; the closest you will get is probably java.util.Scanner.
没有“PrintReader”这样的概念;最接近的可能是java.util.Scanner。
回答by Jon Skeet
PrintWriter
gives more methods (println
), but the most important (and worrying) difference to be aware of is that it swallows exceptions.
PrintWriter
给出了更多的方法 ( println
),但要注意的最重要(也是最令人担忧的)区别是它吞下了异常。
You can call checkError
later on to see whether any errors have occurred, but typically you'd use PrintWriter
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).
您可以checkError
稍后调用以查看是否发生了任何错误,但通常您会使用PrintWriter
诸如写入控制台之类的事情 - 或者在您不想被异常打扰的“快速'n脏”应用程序中(以及在哪里长期可靠性不是问题)。
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'twant exceptions to be swallowed. It would be nice to see BufferedWriter
get the same abilities at some point...
我不确定为什么“额外的格式化能力”和“不吞下异常”方面被捆绑到同一个类中 - 格式化在许多您不希望吞下异常的地方显然很有用。很高兴看到BufferedWriter
在某个时候获得相同的能力......
回答by Kai Huppmann
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:
正如 TofuBeer 的回答中所说,两者都有其专长。要充分利用 PrintWriter(或任何其他编写器)但也使用缓冲写入,您可以将 BufferedWriter 与所需的类似,如下所示:
PrintWriter writer = new PrintWriter(
new BufferedWriter (
new FileWriter("somFile.txt")));
回答by Lioda
PrintWriter just exposes the print methods on any Writer in character mode.
PrintWriter 只是在字符模式下公开任何 Writer 上的打印方法。
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.
根据其缓冲方法,BufferedWriter 比 更有效。它带有一个 newLine() 方法,具体取决于您的系统平台,以正确操作文本文件。
The BufferedReader permits to read a text from file, with bytes converted in characters. It allows to read line by line.
BufferedReader 允许从文件中读取文本,并将字节转换为字符。它允许逐行读取。
There is no PrintReader, you have to choose another Reader implementation according to the format of your input.
没有 PrintReader,您必须根据输入的格式选择另一个 Reader 实现。
回答by AjayLohani
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.
我认为上面已经提到了使用 PrintWriter 背后的原因,但重要的原因之一是您将文件对象直接传递给 PrintWriter 构造函数,这使得它易于使用。
File file=new File(“newfile.txt”);
PrintWriter pw=new PrintWriter(file);
回答by Rohit Goyal
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,
通常,Writer 立即将其输出发送到底层字符或字节流。除非需要提示输出,否则建议将 BufferedWriter 包裹在任何 write() 操作可能开销较大的 Writer 周围,例如 FileWriters 和 OutputStreamWriters。例如,
Note: Text content in the code blocks is automatically word-wrapped
注意:代码块中的文本内容会自动换行
PrintWriter out =
new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
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.
将 PrintWriter 的输出缓冲到文件中。如果没有缓冲,每次调用 print() 方法都会导致字符转换为字节,然后立即写入文件,这可能非常低效。
回答by Vidyut Singhai
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.
BufferedWriter - 将文本写入输出字符流,缓冲字符流中的字符。PrintWriter - 将对象的格式化表示打印到文本输出流。
回答by Syed Maqsood
PrintWriteris the most enhanced Writer to write Character data to a file.
PrintWriter是将字符数据写入文件的最增强的 Writer。
The main advantage of PrintWriter over FileWriter and BufferedWriter is:
PrintWriter 相对于 FileWriter 和 BufferedWriter 的主要优点是:
- PrintWriter can communicate directly with the file, and can communicate via some Writer object also.
- PrintWriter 可以直接与文件通信,也可以通过一些 Writer 对象进行通信。
PrintWriter printwriter = new PrintWriter("blah.txt");
PrintWriter printwriter = new PrintWriter("blah.txt");
(or)
(或者)
FileWriter filewriter = new FileWriter("blah.txt");
PrintWriter printwiter = new PrintWriter(filewriter);
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
我们可以将任何类型的原始数据直接写入文件(因为我们有额外的 PrintWriter 方法重载版本,即 print() 和 println())。
printwriter.print(65); //65
bufferedwriter.write(65); //A
printwriter.println(true); //true