java FileWrite BufferedWriter 和 PrintWriter 组合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15803350/
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
FileWrite BufferedWriter and PrintWriter combined
提问by Ahoura Ghotbi
Ok so I am learning about I/O, and I found the following code in one of the slides. can someone please explain why there is a need to have a FileWrite, BufferedWriter and PrintWriter? I know BufferedWriter is to buffer the output and put it all at once but why would they use FileWriter and PrintWriter ? dont they pretty much do the same with a bit of difference in error handling etc?
好的,我正在学习 I/O,我在其中一张幻灯片中找到了以下代码。有人可以解释为什么需要有 FileWrite、BufferedWriter 和 PrintWriter?我知道 BufferedWriter 是缓冲输出并一次性放入所有内容,但他们为什么要使用 FileWriter 和 PrintWriter ?他们在错误处理等方面几乎没有做同样的事情吗?
And also why do they pass bw
to PrintWriter
?
而且为什么他们传递bw
到PrintWriter
?
FileWriter fw = new FileWriter (file);
BufferedWriter bw = new BufferedWriter (fw);
PrintWriter outFile = new PrintWriter (bw);
回答by Jon Skeet
Presumably they're using a FileWriter
because they want to write to a file. Both BufferedWriter
and PrintWriter
have to be given another writer to write to - you need someeventual destination.
据推测,他们使用 a 是FileWriter
因为他们想写入文件。双方BufferedWriter
并PrintWriter
必须得到另一位作家写-你需要一些最终目的地。
(Personally I don't like FileWriter
as it doesn't let you specify the encoding. I prefer to use FileOutputStream
wrapped in an OutputStreamWriter
, but that's a different matter.)
(我个人不喜欢,FileWriter
因为它不允许您指定编码。我更喜欢使用FileOutputStream
包装在 an 中OutputStreamWriter
,但这是另一回事。)
BufferedWriter
is used for buffering, as you say - although it doesn't buffer allthe output, just a fixed amount of it (the size of the buffer). It creates "chunkier" writes to the underlying writer.
BufferedWriter
用于缓冲,正如您所说 - 尽管它不会缓冲所有输出,但只是固定数量的输出(缓冲区的大小)。它为底层编写器创建了“更笨重”的写入。
As for the use of PrintWriter
- well, that exposes some extra methods such as println
. Personally I dislike it as it swallows exceptions (you have to check explicitly with checkError
, which still doesn't give the details and which I don't think I've everseen used), but again it depends on what you're doing. The PrintWriter
is passed the BufferedWriter
as itsdestination.
至于PrintWriter
-的使用,那暴露了一些额外的方法,例如println
. 我个人不喜欢它,因为它吞下了异常(你必须用 明确检查checkError
,它仍然没有提供细节,而且我认为我从未见过使用过),但这同样取决于你在做什么。将作为其目的地PrintWriter
传递。BufferedWriter
So the code below the section you've shown will presumably write to the PrintWriter
, which will write to the BufferedWriter
, which will (when its buffer is full, or it's flushed or closed) write to the FileWriter
, which will in turn convert the character data into bytes on disk.
因此,您显示的部分下方的代码大概PrintWriter
会写入 ,然后将写入BufferedWriter
,后者将(当其缓冲区已满,或已刷新或关闭时)写入FileWriter
,然后将字符数据转换为磁盘上的字节数。
回答by Ahoura Ghotbi
From the Docs:
从文档:
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。例如,
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() 方法都会导致字符转换为字节,然后立即写入文件,这可能非常低效。
You can understand from this that a BufferedWriter
is an efficient way to write stuff.
从这里你可以理解 aBufferedWriter
是一种有效的写东西的方式。
Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
将文本写入字符输出流,缓冲字符以提供单个字符、数组和字符串的高效写入。
A FileWriter
object is passed to the BufferedWriter
as the intent here is to write to some output file using a BufferedWriter
.
一个FileWriter
对象被传递给 ,BufferedWriter
因为这里的目的是使用BufferedWriter
.
And finally, a PrintWriter
is used for print*
methods like println()
.
最后, aPrintWriter
用于print*
诸如println()
.
回答by Ankit
FileWriter
is simply to write plain text(without formatting) it doesn't use any buffer mechanism, whatever comes its way it just writes.
FileWriter
只是编写纯文本(不带格式),它不使用任何缓冲机制,无论它以何种方式写入。
BufferedWriter
is a wrapper for Writer
classes to allow it to be able to use buffer functionality (to optimize IO).
BufferedWriter
是Writer
类的包装器,以使其能够使用缓冲区功能(以优化 IO)。
PrintWriter
prints formatted text, you can provide format string along with the data to be printed, though it can directly work with any Writer/OutputStream
, just to provide buffering, Writer/OutputStream
is 1st passed to BufferedWriter
then to have formatted text is passed to PrintWriter
PrintWriter
打印格式化文本,您可以提供格式字符串以及要打印的数据,尽管它可以直接与 any 一起使用Writer/OutputStream
,只是为了提供缓冲,Writer/OutputStream
首先传递给BufferedWriter
然后将格式化文本传递给PrintWriter
回答by subodh
PrintWriter from here
PrintWriter 从这里
Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.
将对象的格式化表示打印到文本输出流。此类实现在 PrintStream 中找到的所有打印方法。它不包含写入原始字节的方法,程序应该使用未编码的字节流。
from the above statement it seems the main reason to use PrintWriter is to get the access of all the methods of PrintStream
like println()
,println(char [] x)
etc.
从上面的语句似乎主要理由使用PrintWriter的是得到的所有方法的访问PrintStream
一样println()
,println(char [] x)
等等。
BufferedWriter, You are right It's one of the best way to write to a file because it will buffered the character into the virtual memory before writing to a file directly and came up with a newLine()
method.
BufferedWriter,你说得对,这是写入文件的最佳方式之一,因为它会在直接写入文件之前将字符缓冲到虚拟内存中,并提出了一种newLine()
方法。
FileWriter from here
FileWriter 从这里
FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream
FileWriter 用于写入字符流。要写入原始字节流,请考虑使用 FileOutputStream
.
.
回答by NilsH
Usually, this kind of Writer chaining is about abstraction. PrintWriter
have some useful print
and println
methods that can be convenient if you want to print Strings and lines to a File. Working directly with FileWriter
, you would have to use a more "low level" API. And as you say BufferedWriter
is about buffering. So it's basically a matter of what you want to output to the file, and what level of abstraction you prefer.
通常,这种 Writer 链接是关于抽象的。PrintWriter
有一些有用的print
和println
方法,如果你想打印字符串和线到一个文件,可以很方便。直接使用FileWriter
,您将不得不使用更“低级”的 API。正如你所说的BufferedWriter
是关于缓冲。所以这基本上是你想要输出到文件的问题,以及你喜欢什么样的抽象级别。
回答by Richard Wеrеzaк
The objects are wrapped in this order because you want to use the outermost PrintWriter for its more sophisticated formatting. BufferedWriter must be wrapped on something. So FileWriter, as a result, is what BufferedWriter wraps and is the innermost object.
对象按此顺序包装,因为您希望使用最外层的 PrintWriter 进行更复杂的格式设置。BufferedWriter 必须包裹在某些东西上。所以FileWriter,结果,就是BufferedWriter 包裹的东西,是最里面的对象。