Java:StreamWriter 和 BufferWriter 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6366559/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:36:28  来源:igfitidea点击:

Java: What is the difference between StreamWriter and BufferWriter?

javaparsingwriter

提问by eros

I am parsing file which is 800MB of size (high possibility of more than 2GB). I split it into several files which approximately 1-3kb per file.

我正在解析 800MB 大小的文件(超过 2GB 的可能性很大)。我把它分成几个文件,每个文件大约 1-3kb。

I would like to consult you guys, which is better to use among the two: BufferedWriter and OutputStreamWriter

想请教各位,BufferedWriter 和 OutputStreamWriter 两者用哪个比较好

Any guidance on the right direction is appreciated.

对正确方向的任何指导表示赞赏。

回答by MJB

Ok, since you ask.

好吧,既然你问了。

Writer - an abstract class that concrete implementations of let you write characters/strings. As opposed to raw bytes, which OutputStream implementations do.

Writer - 一个抽象类,它的具体实现让您可以编写字符/字符串。与原始字节相反,OutputStream 实现会这样做。

FileWriter - a concrete implementation that lets you write to a File. Weakness: The encoding of the characters is hard-coded to be the default Locale, for example usually Windows-1252 on Windows, and UTF-8 on Linux.

FileWriter - 一个让您写入文件的具体实现。弱点:字符的编码被硬编码为默认的语言环境,例如在 Windows 上通常是 Windows-1252,在 Linux 上通常是 UTF-8。

To overcome this, many people start with an OutputStream (maybe a FileOutputStream) and then convert it into a Writer using OutputStreamWriter, because the constructor lets you set the encoding.

为了克服这个问题,许多人从一个 OutputStream(可能是一个 FileOutputStream)开始,然后使用 OutputStreamWriter 将它转换成一个 Writer,因为构造函数允许您设置编码。

Example:

例子:

OutputStream os = new FileOutputStream("turnip");
Writer writer = new OutputStreamWriter(os,"UTF-8");
writer.write("This string will be written as UTF-8");

Now, with OutputStreams/Writers (and their inverse classes InputStream/Readers), it is often useful in addition to wrap a BufferedWriter around them.

现在,对于 OutputStreams/Writers(以及它们的逆类 InputStream/Readers),除了将 BufferedWriter 包裹在它们周围之外,它通常很有用。

continuing from example

从例子继续

writer=new BufferedWriter(writer);
writer.write("Another string in UTF-8");

What does this do? A BufferedWriter basically provides a memory buffer. Everything you write is first stored in memory and then flushed as necessary to disk (or whever). This often provides dramatic performance improvements. To show yourself this, just create a loop of say 100,000 writes without the BufferedWriter, time it, and compare that to the Buffered version.

这有什么作用?BufferedWriter 基本上提供了内存缓冲区。您写的所有内容首先存储在内存中,然后根据需要刷新到磁盘(或任何地方)。这通常会提供显着的性能改进。为了向您展示这一点,只需创建一个不使用 BufferedWriter 写入 100,000 次的循环,计时,并将其与 Buffered 版本进行比较。

回答by gmhk

There is no Stream writer in Java

Java 中没有 Stream 编写器

If you want to learn about the Input and output Stream Best place to learn is the following link

如果您想了解输入和输出流,最好的学习地点是以下链接