Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileInputStream vs FileReader

Tags:

java

io

writer

FileReader rd=new FileReader("new.mp4"); FileWriter wr=new FileWriter("output.mp4"); int ch; while((ch=rd.read())!=-1)   wr.write(ch);  wr.flush(); wr.close(); 

When I use the FileReader and FileWriter to read and write an mp4 file, the output.mp4 file can't be rendered well. But when I use FileInputStream and FileOutputStream instead it worked well.

So can I conclude FileReader and FileWriter are only for reading and writing text?

like image 863
saravanan Avatar asked Mar 01 '11 13:03

saravanan


People also ask

What is the difference between FileInputStream and BufferedInputStream?

A BufferedInputStream reads from another InputStream , but a FileInputStream reads from a file1.

What is the difference between FileInputStream and Fileoutputstream?

InputStream − This is used to read (sequential) data from a source. OutputStream − This is used to write data to a destination.

What is difference between FileReader and BufferedReader?

FileReader is used to read a file from a disk drive whereas BufferedReader is not bound to only reading files. It can be used to read data from any character stream.

Why do we use FileInputStream?

FileInputStream class is useful to read data from a file in the form of sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.


2 Answers

Yes, your conclusion is correct subclasses of Reader and Writer are for reading/writing text content. InputStream / OutputStream are for binary content. If you take a look at the documentation:

Reader - Abstract class for reading character streams

InputStream - Abstract class is the superclass of all classes representing an input stream of bytes.

like image 147
Bozho Avatar answered Oct 14 '22 06:10

Bozho


FileReader (and indeed anything extending Reader) is indeed for text. From the documentation of Reader:

Abstract class for reading character streams.

(Emphasis mine.) Look at the API and you'll see it's all to do with text - char instead of byte all over the place.

InputStream and OutputStream are for binary data, such as mp4 files.

Personally I would avoid FileReader altogether though, as it always uses the system default character encoding - at least before Java 11. Instead, use InputStreamReader around a FileInputStream... but only when you want to deal with text. (Alternatively, use Files.newBufferedReader.)

As an aside, that's a very inefficient way of copying from an input to an output... use the overloads of read and write which read into or write from a buffer - either a byte[] or a char[]. Otherwise you're calling read and write for every single byte/character in the file.

You should also close IO streams in finally blocks so they're closed even if an exception is thrown while you're processing them.

like image 23
Jon Skeet Avatar answered Oct 14 '22 04:10

Jon Skeet