Java: Difference between PrintStream and PrintWriter

JavaIoPrintwriterPrintstream

Java Problem Overview


What is the difference between PrintStream and PrintWriter? They have many methods in common due to which I often mix these two classes up. Moreover, I think we can use them for exactly the same things. But there has to be a difference, otherwise, there would have been only one class.

I have searched the archives, but couldn't find this question.

Java Solutions


Solution 1 - Java

This might sound flippant, but PrintStream prints to an OutputStream, and PrintWriter prints to a Writer. Ok, I doubt I'll get any points for stating the obvious. But there's more.

So, what is the difference between an OutputStream and a Writer? Both are streams, with the primary difference being a OutputStream is a stream of bytes while a Writer is a stream of characters.

If an OutputStream deals with bytes, what about PrintStream.print(String)? It converts chars to bytes using the default platform encoding. Using the default encoding is generally a bad thing since it can lead to bugs when moving from one platform to another, especially if you are generating the file on one platform and consuming it on another.

With a Writer, you typically specify the encoding to use, avoiding any platform dependencies.

Why bother having a PrintStream in the JDK, since the primary intent is to write characters, and not bytes? PrintStream predates JDK 1.1 when Reader/Writer character streams were introduced. I imagine Sun would have deprecated PrintStream if only for the fact it is so widely used. (After all, you wouldn't want each call to System.out to generate a deprecated API warning! Also, changing the type from PrintStream to PrintWriter on the standard output streams would have broken existing applications.)

Solution 2 - Java

Since JDK 1.4 it's possible to specify the character encoding for a PrintStream. Thus, the differences between PrintStream and PrintWriter are only about auto flushing behavior and that a PrintStream cannot wrap a Writer.

Solution 3 - Java

Writers like PrintWriter are for text output, streams are for binary output. The writers handle character set stuff for you. Streams don't because it's assumed that you don't want that sort of conversion, which would mess up your binary data, and would be using a writer if you did.

Solution 4 - Java

You can write raw bytes to a Stream and not to a Writer. The PrintWriter javadoc lists the other differences (most importantly, being able to set an encoding on a stream so it can interpret the raw bytes I'd say).

Solution 5 - Java

from core java by Horstmann > Java veterans might wonder whatever happened to the PrintStream class and to System.out. In Java 1.0, the PrintStream class simply truncated all Unicode characters to ASCII characters by dropping the top byte. (At the time, Unicode was still a 16-bit encoding.) Clearly, that was not a clean or portable approach, and it was fixed with the introduction of readers and writers in Java 1.1. For compatibility with existing code, System.in, System.out, and System.err are still input/output streams, not readers and writers. But now the PrintStream class internally converts Unicode characters to the default host encoding in the same way the PrintWriter does. Objects of type PrintStream act exactly like print writers when you use the print and println methods, but unlike print writers they allow you to output raw bytes with the write(int) and write(byte[]) methods.

Solution 6 - Java

Printwriter is an enhancement of printstream.

I.E. printstream for a specific purpose.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartijn CourteauxView Question on Stackoverflow
Solution 1 - JavamdmaView Answer on Stackoverflow
Solution 2 - JavaRenan MozoneView Answer on Stackoverflow
Solution 3 - JavasblundyView Answer on Stackoverflow
Solution 4 - JavaSimon GroenewoltView Answer on Stackoverflow
Solution 5 - JavanichijouView Answer on Stackoverflow
Solution 6 - JavaSpooView Answer on Stackoverflow