What is the difference between OutputStream and Writer?
Asked Answered
F

5

14

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

Furculum answered 30/5, 2012 at 12:42 Comment(2)
Not getting the close votes on this. It's a real question, with a real answer.Hejaz
My close vote is because the real answer is in the Javadoc, where the OP should have looked first, so the question has no permanent value.Brendis
O
19

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.

Overwinter answered 30/5, 2012 at 12:49 Comment(3)
I have a question on this: The PrintStream used in System.out.println is essentially an OutputStream and need to handle encodings as well, when I enter UTF-8 characters. So shouldn't it be an PrintWriter instead? Thank you for your explanation.Mononuclear
Very good question. I suspect System.out being a PrintStream is a legacy heritage and could not be easily changed afterwards. As the documentation explains, PrintStream always uses the default encoding and PrintWriter is actually recommended over using PrintStream.Overwinter
Thanks for your guessed explanation, I always forget how old Java is, and legacy compliance is an too often underestimated burden :), or as there is a saying in german: exceptions confirm the rulesMononuclear
H
8

OutputStream classes writes to the target byte by byte where as Writer classes writes to the target character by character

Hurtful answered 30/5, 2012 at 12:44 Comment(1)
It's worth noting that Character in Java is a misnomer, as it actually represents an UTF-16 Code Unit, and not a Unicode Character or Code Point.Amin
A
3

An OutputStream is a stream that can write information. This is fairly general, so there are specialized OutputStream for special purposes like writing to files. A stream can only write arrays of bytes.

Writers provide more flexibility in that they can write characters and even strings while taking a special encoding into account.

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

Alcheringa answered 30/5, 2012 at 12:46 Comment(9)
You can never write characters to a stream, they have to be encoded as bytes first, which in Java means they will always go to an OutputStream, for example via an OutputStreamWriter.Amin
I didn't say you could write characters to a stream - I said you can use a Writer to write a string to a stream, which you can't do when using only a stream. Edited my answer to make this more clear.Alcheringa
A string is a sequence of characters (or in Java a sequence of code units), so no, you can't write a string to a stream, they have to be encoded as bytes first.Amin
Please quote the line where I'm saying you could write strings to a stream.Alcheringa
You edited it, and then you said "I said you can use a Writer to write a string to a stream"Amin
Yes, I said so because that (from the developer's point of view and judging from the result) is exactly what's happening. I have a string, which I want to write to a file, for example. So I can use the stream directly only if I convert the string to a byte array first. Using a Writer on the stream allows me to "write a string to a stream", even though technically the writer does the byte array conversion implicitly and writes a byte array to the stream. The result is the same, however.Alcheringa
You don't need to explain to me how it works. Please quote the line where i was incorrect instead of worrying about having your answer challenged and demanding quotes.Amin
I just didn't see why you wrote your first comment in the first place - the only reason for me seemed to be that my answer was incorrect and I didn't see where. So I was asking you to tell me where I was incorrect. That's it.Alcheringa
If nothing else, it certainly implied that you can write characters to a stream, a common misunderstanding which is incorrect in any programming language, since characters always have to be represented as bytes when coming in or going out of a system and thus have to be encoded or decoded.Amin
F
1

OutputStream uses bare bytes, whereas Writer uses encoded charaters.

Fortaleza answered 30/5, 2012 at 12:45 Comment(1)
Actually a Writer uses UTF-16 Code Units, and not Encoded CharactersAmin
R
1

The Reader/Writer class hierarchy is character-oriented, and the Input Stream/Output Stream class hierarchy is byte-oriented. Basically there are two types of streams.Byte streams that are used to handle stream of bytes and character streams for handling streams of characters.In byte streams input/output streams are the abstract classes at the top of hierarchy,while writer/reader are abstract classes at the top of character streams hierarchy.

More details here

Cheers!!!

Resilience answered 30/5, 2012 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.