Search code examples
javaiowriter

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 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?


Solution

  • 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.