PrintStream was the original bridge to deal with encoding characters and other datatypes. If you look at the javadoc for java.io.OutputStream you'll see methods only for writing two distinct data types: byte and int.

In early versions of the JDK (1.0.x), when you wanted to write characters, you could do one of two things, write bytes to an output stream (which are assumed to be in the system default character set):

outputStream.write("foobar".getBytes());

or wrap another outputStream in a PrintStream:

PrintStream printStream = new PrintStream(outputStream);
printStream.write("foobar");

See the difference? PrintStream is handling the character conversion to bytes, as well as encoding (the constructor call above uses the system default encoding, but you could pass it as a parameter). It also provides convenience methods for writing double, boolean, etc....

In fact System.out and System.err are defined as PrintStream instances.

Along comes JDK 1.1, and they realize they need a better way to deal with pure character data, since PrintStream still has the byte based methods for writing. So they introduced the Writer abstract class to deal strictly with char, String and int data.

PrintWriter adds methods for other types like double, boolean, etc...

Nowadays PrintWriter also has format() / printf() methods for format printing, etc...

As a general rule, if you're writing character data, use Writer instances. If you're writing binary (or mixed) data use OutputStream instances.

Answer from Matt on Stack Overflow
Top answer
1 of 3
29

PrintStream was the original bridge to deal with encoding characters and other datatypes. If you look at the javadoc for java.io.OutputStream you'll see methods only for writing two distinct data types: byte and int.

In early versions of the JDK (1.0.x), when you wanted to write characters, you could do one of two things, write bytes to an output stream (which are assumed to be in the system default character set):

outputStream.write("foobar".getBytes());

or wrap another outputStream in a PrintStream:

PrintStream printStream = new PrintStream(outputStream);
printStream.write("foobar");

See the difference? PrintStream is handling the character conversion to bytes, as well as encoding (the constructor call above uses the system default encoding, but you could pass it as a parameter). It also provides convenience methods for writing double, boolean, etc....

In fact System.out and System.err are defined as PrintStream instances.

Along comes JDK 1.1, and they realize they need a better way to deal with pure character data, since PrintStream still has the byte based methods for writing. So they introduced the Writer abstract class to deal strictly with char, String and int data.

PrintWriter adds methods for other types like double, boolean, etc...

Nowadays PrintWriter also has format() / printf() methods for format printing, etc...

As a general rule, if you're writing character data, use Writer instances. If you're writing binary (or mixed) data use OutputStream instances.

2 of 3
10

From the Javadoc for PrintWriter:

Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.

Think of it this way: a PrintStream sits on top of some OutputStream. Since output streams deal with bytes rather than characters, the PrintStream must take responsibility for encoding characters into bytes. The OutputStream 'merely' writes the bytes out to a file/console/socket whatever.

A PrintWriter, on the other hand, sits on top of a Writer. Since the Writer is responsible for encoding characters into bytes, the PrintWriter does not do encoding. I just knows about newlines etc. (Yes, PrintWriters do have constructors that take Files and OutputStreams, but those are simply conveniences. For example, PrintWriter(OutputStream).

Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.

BTW, In case you are thinking that the PrintWriter really doesn't have much utility, remember that both PrintWriter and PrintStream absorb IOExceptions from printing logic.

🌐
Baeldung
baeldung.com › home › java › java io › printstream vs printwriter in java
PrintStream vs PrintWriter in Java | Baeldung
January 8, 2024 - Based on how these classes process text data internally, a character stream class such as PrintWriter handles this content better when doing I/O operations with text. Besides, translating the data into Unicode during the encoding process of ...
Discussions

Difference between Printstream outputting bytes and Printwriter outputting characters in Java
What's the difference between printstream and printwriter in terms of their outputs? Aren't both bytes and characters primitive numerical types? And they both produce human-readable text, right? So why have both of them or use one over the other? What's the diff btw raw bytes and characters · Archived post. New comments cannot be posted and votes cannot be cast. Share ... A byte and a character are exactly the same thing in most cases (UTF-8). However in Java... More on reddit.com
🌐 r/learnprogramming
1
2
January 12, 2022
PrintWriter System.out differences...?
System.out is of PrintStream type and javadoc explains it verry well how it works. Basicaly System.out si configured to auto flush. https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/io/PrintStream.html More on reddit.com
🌐 r/learnjava
2
13
October 20, 2020
PrintStream v/s PrintWriter - Oracle Forums
For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you More on forums.oracle.com
🌐 forums.oracle.com
July 5, 2005
what difference between PrintWriter and PrintStream?
Find answers to what difference between PrintWriter and PrintStream? from the expert community at Experts Exchange More on experts-exchange.com
🌐 experts-exchange.com
March 3, 2008
Top answer
1 of 6
135

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

2 of 6
22

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.

🌐
Coderanch
coderanch.com › t › 743496 › java › PrintStream-PrintWriter
PrintStream versus PrintWriter (I/O and Streams forum at Coderanch)
From the beginning the rule in Java I/O was that a Writer would convert between chars (in the code) and bytes (in the external environment), whereas a Stream would only traffic in bytes. So having a PrintStream which used an encoding (old name) or charset (new name) to map chars to bytes would ...
🌐
Quora
quora.com › What-is-the-difference-between-a-print-stream-and-print-writer-in-Java
What is the difference between a print stream and print writer in Java? - Quora
Answer: PrintStream prints to an OutputStream, and PrintWriter prints to a Writer. Both are streams, but the difference is that OutputStream is a stream of bytes while a Writer is a stream of characters.
🌐
O'Reilly
oreilly.com › library › view › java-for-dummies › 9781118239742 › a64_07_9781118239742-ch04.html
PrintStream Class - Java For Dummies Quick Reference [Book]
June 5, 2012 - PrintStream and PrintWriter have nearly identical methods. The primary difference is that PrintStream writes raw bytes in the machine’s native character format, and PrintWriter converts bytes to recognized encoding schemes.
Author   Doug Lowe
Published   2012
Pages   288
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › PrintStream.html
PrintStream (Java Platform SE 7 )
All characters printed by a PrintStream are converted into bytes using the platform's default character encoding. The PrintWriter class should be used in situations that require writing characters rather than bytes.
🌐
Reddit
reddit.com › r/learnprogramming › difference between printstream outputting bytes and printwriter outputting characters in java
r/learnprogramming on Reddit: Difference between Printstream outputting bytes and Printwriter outputting characters in Java
January 12, 2022 - What's the difference between printstream and printwriter in terms of their outputs? Aren't both bytes and characters primitive numerical types? And they both produce human-readable text, right? So why have both of them or use one over the other? What's the diff btw raw bytes and characters · Archived post. New comments cannot be posted and votes cannot be cast. Share ... A byte and a character are exactly the same thing in most cases (UTF-8). However in Java, by default a character is 2 bytes (UTF-16).
Find elsewhere
🌐
Coderanch
coderanch.com › t › 380909 › java › difference-printStream-PrintWriter
what is the difference between printStream and PrintWriter? (Java in General forum at Coderanch)
September 12, 2006 - PrintStream is an OutputStream, which means it is mainly intended for processing bytes. PrintWriter is a Writer, meaning it is intended for processing characters. As the "printing" operations that both classes perform are more to do with characters, PrintWriter is usually preferred over PrintStream.
🌐
Reddit
reddit.com › r/learnjava › printwriter system.out differences...?
r/learnjava on Reddit: PrintWriter System.out differences...?
October 20, 2020 -

So I made some code to test a theory, and it disproved my theory. Here it is:

import java.io.PrintWriter;

public class Main {
    public static void main(String[] args) {
        PrintWriter writer = new PrintWriter(System.out);

        System.out.println("HI");
        writer.println("HI");
        writer.flush();
    }
}

Now here's the context for my question. I was taught that System.out is a stream object, and that the console grabs from the System.out stream and puts those contents on its stream. I was then taught that PrintWriter is an class that functions essentially identically to System.out, except that you can direct your destination stream. So I hypothesized, "If you made a PrintWriter object and sent the System.out stream in as its destination, would that be identical to just using System.out?

As it turns out, no, but I still don't fully understand why. What I do understand is that if I use the flush() method, it does essentially work the same. So my question is this:

When you use System.out.println() , does the method println() also tell the console to grab from the buffer so its not left in the System.out stream? And from the other perspective, when you use writer.println() (writer being the PrintWriter object in this context), does the println() method NOT tell the destination stream to grab from the buffer (until the flush() method is used)?

Any information helps, feel free to tell me that my interpretation is wrong or that my hypothesis is wrong. Or you can tell me anything really, tell me how your day is going. Any information helps, the more I know the better a programmer I can become :)

🌐
Oracle
forums.oracle.com › ords › apexds › post › printstream-v-s-printwriter-5996
PrintStream v/s PrintWriter - Oracle Forums
July 5, 2005 - In the folloowing code, I have used PrintStream to output some text to a file. package src; import java.io.*; public class Sample { public static void main(String[] args) { PrintStream out = nul...
🌐
CodingTechRoom
codingtechroom.com › question › java-printstream-vs-printwriter
What Are the Key Differences Between PrintStream and PrintWriter in Java? - CodingTechRoom
PrintStream is designed for outputting binary data and can handle both byte streams and character streams. PrintWriter is specifically for writing character data and is designed to handle various character encodings, making it more suitable for text output.
🌐
CodingTechRoom
codingtechroom.com › tutorial › java-java-printstream-vs-printwriter
Java PrintStream vs PrintWriter: A Comprehensive Guide - CodingTechRoom
PrintStream is better for binary or mixed data, whereas PrintWriter is preferable for character data, offering better control over character encoding and exception handling. Explore more about BufferedWriter for efficient file writing. Learn about other Java I/O classes like FileReader and ...
🌐
Electro4u
electro4u.net › blog › comparing-printstream-and-printwriter-in-java--1327
Comparing PrintStream and PrintWriter in Java
The PrintStream class is better suited for writing primitive data values, such as numbers and boolean values, to standard output streams. It is also more efficient than the PrintWriter class since it can write binary data. On the other hand, the PrintWriter class is better suited for writing ...
🌐
Quora
quora.com › Under-which-conditions-are-PrintWriter-and-PrintStream-used-in-Java
Under which conditions are PrintWriter and PrintStream used in Java? - Quora
March 12, 2015 - Answer (1 of 3): Printstream is used when you are going to send file over network or writing it to disk and you are going to operate on streams/bytes not in text based encoding. When it is text/character based file and you are going to write ...
🌐
Way2Java
way2java.com › home › printstream vs printwriter
PrintStream vs PrintWriter
November 7, 2015 - Leave a Comment / I/O streams in java, Print Streams / By S. Nageswara Rao, Corporate Trainer · These two streams are carriers of data to a destination. Both look alike, but they differ in some finer concepts. Basically, the OutputStream class (super class of PrintStream) is an abstract class, ...
🌐
Experts Exchange
experts-exchange.com › questions › 20029535 › what-difference-between-PrintWriter-and-PrintStream.html
Solved: what difference between PrintWriter and PrintStream? | Experts Exchange
March 3, 2008 - Forgot to add one final usage note. In JDK 1.1, the public constructors for PrintStream were deprecated to encourage use of the new PrintWriter. Experience indicates that replacing PrintStream with PrintWriter is not always practical. So JDK 1.2 removed the deprecations from the PrintStream constructors.
🌐
Learn IT University
learn-it-university.com › home › understanding the key differences between printstream and printwriter in java
Understanding the Key Differences Between PrintStream and PrintWriter in Java - Learn IT University
July 21, 2024 - By the end of this article, you ... distinction lies in what each class prints to: PrintStream writes data to an OutputStream, while PrintWriter operates through a Writer....
🌐
Cleverence
cleverence.com › articles › oracle-documentation › printstream-java-platform-se-8-4821
Java PrintStream (SE 8) Guide: Methods, Encoding, System.out, Examples
March 8, 2026 - PrintWriter is the character-stream sibling of PrintStream. While PrintStream works with bytes and encodes characters for you, PrintWriter works directly with characters and wraps a Writer.