saravanan saravanan - 5 months ago 32
Java Question

FileInputStream vs FileReader

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 mp4 file ....
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 writer are only for reading and writing only text ?

Answer

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.