What is the difference between OutputStream and Writer?

Ani picture Ani · May 30, 2012 · Viewed 17.4k times · Source

Can someone explain me the difference between OutputStream and Writer? Which of these classes should I work with?

Answer

Vincent Robert picture Vincent Robert · May 30, 2012

Streams work at the byte level, they can read (InputStream) and write (OutputStream) bytes or list of bytes to a stream.

Reader/Writers add the concept of character on top of a stream. Since a character can only be translated to bytes by using an Encoding, readers and writers have an encoding component (that may be set automatically since Java has a default encoding property). The characters read (Reader) or written (Writer) are automatically converted to bytes by the encoding and sent to the stream.