简体   繁体   中英

FileWriter vs FileOutputStream in Java

I'm little confused about FileWriter and FileOutputStream . As I see source code of FileWriter there are just 4 constructors and each constructor is calling FileOutputStream 's constructor.

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.

How FileWriter can make difference? Even it still calling FileOutputStream 's constructor without any changing.

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.

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 me to allow me to specify the character encoding (as FileWriter always uses the platform default encoding, annoyingly).

Basically, think of FileWriter is a simple way of letting you write:

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 .

FileOutputStream is to write primitive types of data, like int , while FileWriter is to write character-oriented data.

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 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 fr=new FileWriter("C:\\");
fr.write("Hello");
fr.close();

Do not forget to throw any exceptions if needed.

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.

Actually, the question is when do use Reader/Writer and when Stream?

How to Decide Which to use:

1.If you are dealing with binary data (e.g. an image) use Streams.
2.If you are using non-ASCII Unicode characters, use Readers/Writers.
3.If you are using ordinary ASCII text you can (usually) use either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM