Java 中的 FileWriter 与 FileOutputStream
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36250571/
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
FileWriter vs FileOutputStream in Java
提问by Jon Skeet
I'm little confused about FileWriter
and FileOutputStream
. As I see source code of FileWriterthere are just 4 constructors and each constructor is calling FileOutputStream
's constructor.
我对FileWriter
和有点困惑FileOutputStream
。正如我看到的 FileWriter 的源代码只有 4 个构造函数,每个构造函数都在调用FileOutputStream
的构造函数。
public FileWriter(String fileName) throws IOException {
super(new FileOutputStream(fileName));
}
public FileWriter(String fileName, boolean append) throws IOException {
super(new FileOutputStream(fileName, append));
}
public FileWriter(File file) throws IOException {
super(new FileOutputStream(file));
}
public FileWriter(File file, boolean append) throws IOException {
super(new FileOutputStream(file, append));
}
public FileWriter(FileDescriptor fd) {
super(new FileOutputStream(fd));
}
After searching difference between them I found mentioned here.
在搜索它们之间的差异后,我发现这里提到了。
FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.
FileOutputStream 用于写入原始字节流,例如图像数据。要写入字符流,请考虑使用 FileWriter。
How FileWriter
can make difference? Even it still calling FileOutputStream
's constructor without any changing.
怎样FileWriter
才能有所作为?即使它仍然调用FileOutputStream
的构造函数而没有任何改变。
回答by Jon Skeet
FileWriter
is a Writer
. It's about writing text- and it happens to be writing it to a file. It does that by holding a reference to a FileOutputStream
, which is created in the FileWriter
constructor and passed to the superclass constructor.
FileWriter
是一个Writer
。这是关于编写文本- 它恰好是将其写入文件。它通过持有对 a 的引用来实现FileOutputStream
,该引用在FileWriter
构造函数中创建并传递给超类构造函数。
FileOutputStream
is an OutputStream
. It's about writing binary data. If you want to write text to it, you need something to convert that text to binary data - and that's exactly what FileWriter
does. Personally I prefer to use FileOutputStream
wrapped in an OutputStreamWriter
by meto allow me to specify the character encoding (as FileWriter
always uses the platform default encoding, annoyingly).
FileOutputStream
是一个OutputStream
。这是关于写入二进制数据。如果你想向它写入文本,你需要一些东西来将该文本转换为二进制数据——这正是FileWriter
它的作用。就我个人而言,我更喜欢使用FileOutputStream
包装在一个OutputStreamWriter
由我来允许我指定字符编码(因为FileWriter
总是使用平台默认编码,很烦人)。
Basically, think of FileWriter
is a simple way of letting you write:
基本上,think ofFileWriter
是一种让您编写的简单方法:
Writer writer = new FileWriter("test.txt");
instead of
代替
Writer writer = new OutputStreamWriter(new FileOutputStream("test.txt"));
Except I'd normally recommend using the overload of the OutputStreamWriter
constructor that accepts a Charset
.
除非我通常建议使用OutputStreamWriter
接受 a的构造函数的重载Charset
。
回答by Willy du Preez
A FileOutputStream
writes bytes directly. A FileWriter
encapsulates a FileOutputStream
(by creating it in the FileWriter
constructor as in your question) and provides convenience methods to write characters and Strings.
AFileOutputStream
直接写入字节。AFileWriter
封装了 a FileOutputStream
(通过在FileWriter
构造函数中创建它,如您的问题一样)并提供方便的方法来编写字符和字符串。
回答by Scion11
FileOutputStream
is to write primitive types of data, like int
, while FileWriter
is to write character-oriented data.
FileOutputStream
是编写原始类型的数据,例如int
,而FileWriter
编写面向字符的数据。
FileOutputStream
does not come with methods to deal with strings. If you want to use FileOutputStream
to write a string to a file, you have to go like:
FileOutputStream
没有附带处理字符串的方法。如果要使用FileOutputStream
将字符串写入文件,则必须执行以下操作:
FileOutputStream fos=new FileOutputStream();
String str="Hello";
byte b[]=str.getBytes();
fos.write(b);
fos.close();
In Filewriter
there is no conversion between string and byte array. You could simply use:
在Filewriter
字符串和字节数组之间没有转换。你可以简单地使用:
FileWriter fr=new FileWriter("C:\");
fr.write("Hello");
fr.close();
Do not forget to throw any exceptions if needed.
如果需要,不要忘记抛出任何异常。