What is the difference between OutputStream and OutputWriter?

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

+6
source share
5 answers

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

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

+11
source

the OutputStream classes write to the target byte by byte , where the Writer classes write the target character with>

+7
source

An OutputStream is a stream that can record information. This is quite common, so for special purposes, such as writing to files, there are specialized OutputStream . A stream can only write arrays of bytes.

Writer provide more flexibility in that they can write characters and even strings when considering special encoding.

Which one to take is really a question of what you want to write. If you already have bytes, you can use the stream directly. If you have characters or strings, you need to either convert them to bytes yourself if you want to write them to a stream, or you need to use Writer , which does the job for you.

+2
source

OutputStream uses bare bytes, while Writer uses encoded characters.

0
source

The Reader / Writer class hierarchy is character-oriented, and the input / output class hierarchy is byte-oriented. There are basically two types of threads. Byte streams that are used to process a stream of bytes and character streams to process character streams. Byte stream I / O streams use abstract classes at the top of the hierarchy, while writer / reader classes use abstract classes at the top of the character stream hierarchy.

More here

Hurrah!!!

0
source

Source: https://habr.com/ru/post/917007/


All Articles