简体   繁体   English

FileWrite BufferedWriter和PrintWriter相结合

[英]FileWrite BufferedWriter and PrintWriter combined

Ok so I am learning about I/O, and I found the following code in one of the slides. 好的,我正在学习I / O,我在其中一张幻灯片中找到了以下代码。 can someone please explain why there is a need to have a FileWrite, BufferedWriter and PrintWriter? 有谁可以解释为什么需要有FileWrite,BufferedWriter和PrintWriter? I know BufferedWriter is to buffer the output and put it all at once but why would they use FileWriter and PrintWriter ? 我知道BufferedWriter是缓冲输出并将其全部放在一起,但为什么他们会使用FileWriter和PrintWriter? dont they pretty much do the same with a bit of difference in error handling etc? 他们几乎没有做同样的错误处理等一点点差异?

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);

Presumably they're using a FileWriter because they want to write to a file. 据推测,他们正在使用FileWriter因为他们想要写入文件。 Both BufferedWriter and PrintWriter have to be given another writer to write to - you need some eventual destination. 必须给BufferedWriterPrintWriter写入另一个写入程序 - 您需要一些最终目标。

(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因为它不允许你指定编码。我更喜欢使用包装在OutputStreamWriter FileOutputStream ,但这是另一回事。)

BufferedWriter is used for buffering, as you say - although it doesn't buffer all the output, just a fixed amount of it (the size of the buffer). 正如你所说, BufferedWriter用于缓冲 - 虽然它不缓冲所有输出,只是它的固定量(缓冲区的大小)。 It creates "chunkier" writes to the underlying writer. 它为底层作者创建了“更大”的写作。

As for the use of PrintWriter - well, that exposes some extra methods such as println . 至于PrintWriter的使用 - 那么,暴露了一些额外的方法,如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 ever seen used), but again it depends on what you're doing. 我个人不喜欢它,因为它吞下异常(你必须明确检查checkError ,它仍然没有提供详细信息,我认为我从未见过使用过),但这又取决于你在做什么。 The PrintWriter is passed the BufferedWriter as its destination. 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 ,而FileWriter将转换为字符数据到磁盘上的字节数。

From the Docs : 来自Docs

In general, a Writer sends its output immediately to the underlying character or byte stream. 通常,Writer会立即将其输出发送到基础字符或字节流。 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. 除非需要提示输出,否则建议将BufferedWriter包装在任何write()操作可能代价高昂的Writer周围,例如FileWriters和OutputStreamWriters。 For example, 例如,

 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));

will buffer the PrintWriter's output to the file. 将PrintWriter的输出缓冲到文件。 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. 如果没有缓冲,每次调用print()方法都会导致字符转换为字节,然后立即写入文件,这可能效率很低。

You can understand from this that a BufferedWriter is an efficient way to write stuff. 你可以从中理解BufferedWriter是一种编写东西的有效方法。

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() . 最后, PrintWriter用于print*方法,如println()

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). BufferedWriterWriter类的包装器,允许它使用缓冲区功能(优化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打印格式化文本,你可以提供格式字符串以及要打印的数据,虽然它可以直接使用任何Writer/OutputStream ,只是为了提供缓冲, Writer/OutputStream首先传递给BufferedWriter然后将格式化文本传递给PrintWriter

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. 此类实现PrintStream中的所有打印方法。 It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams. 它不包含写入原始字节的方法,程序应使用未编码的字节流。

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. FileWriter用于编写字符流。 For writing streams of raw bytes, consider using a FileOutputStream 要编写原始字节流,请考虑使用FileOutputStream

.

Usually, this kind of Writer chaining is about abstraction. 通常,这种Writer链接是关于抽象的。 PrintWriter have some useful print and println methods that can be convenient if you want to print Strings and lines to a File. PrintWriter有一些有用的printprintln方法,如果要将字符串和行打印到文件中,这些方法很方便。 Working directly with FileWriter , you would have to use a more "low level" API. 直接使用FileWriter ,您将不得不使用更“低级”的API。 And as you say BufferedWriter is about buffering. 正如你所说BufferedWriter是关于缓冲的。 So it's basically a matter of what you want to output to the file, and what level of abstraction you prefer. 所以它基本上是你要输出到文件的内容,以及你喜欢什么级别的抽象。

The objects are wrapped in this order because you want to use the outermost PrintWriter for its more sophisticated formatting. 对象按此顺序包装,因为您希望使用最外面的PrintWriter进行更复杂的格式化。 BufferedWriter must be wrapped on something. BufferedWriter必须包装好。 So FileWriter, as a result, is what BufferedWriter wraps and is the innermost object. 因此,FileWriter就是BufferedWriter包装的内容,也是最内层的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PrintWriter 与 BufferedWriter - PrintWriter vs BufferedWriter 构造函数PrintWriter(BufferedWriter)未定义 - The constructor PrintWriter(BufferedWriter) is undefined 带有套接字的BufferedWriter与PrintWriter? - BufferedWriter vs PrintWriter with sockets? 我应该使用PrintWriter包装BufferedWriter吗? - should I use PrintWriter to wrap BufferedWriter? new PrintWriter(new BufferedWriter(new PrintWriter(s.getOutputStream,true))) - new PrintWriter(new BufferedWriter(new PrintWriter(s.getOutputStream, true))) Java客户端/服务器 - 使用BufferedWriter而不是PrintWriter - Java Client/Server - Using BufferedWriter instead of PrintWriter PrintWriter 如何写入字节流,而 BufferedWriter 不能? - How PrintWriter can write to byte stream, but BufferedWriter cannot? 为什么PrintWriter可以用于输出到Process,而BufferedWriter不起作用? - Why does PrintWriter work for outputting to Process, but BufferedWriter does not? java.io.PrintWriter和java.io.BufferedWriter之间的区别? - Difference between java.io.PrintWriter and java.io.BufferedWriter? BufferedWriter / PrintWriter / OutputStreamWriter不会刷新缓冲区,直到调用.close()为止(通过套接字输出流) - BufferedWriter/PrintWriter/OutputStreamWriter don't flush buffer until .close() is called (over socket outputstream)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM