I think you have three options,

Option 1: Wrap your PrintStream around a FileOutputStream in append mode.

Option 2: Make a single PrintStream global variable.

Option 3: Use System.setOut(PrintStream) - then you can just call System.out.printf everywhere to write to your PrintStream (honestly though, this is just another way to do option 2).

Answer from Elliott Frisch on Stack Overflow
🌐
Reddit
reddit.com › r/the_crew › i made the printstream skin from cs:go into a livery. lmk what you think
r/The_Crew on Reddit: I made the Printstream skin from CS:GO into a livery. lmk what you think
June 27, 2024 - So people really loved my CSGO Wraps on cars, So I made more! ... Few days ago I uploaded the first skin in PRINTSTREAM collection, the AK-47. Here's another 2.
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.

🌐
CS2 Skins
stash.clash.gg › family › Printstream
All Printstream Skins (CS2 & CS:GO) - Printstream Collection
Bloodhound Gloves Broken Fang Gloves Driver Gloves Hand Wraps Hydra Gloves Moto Gloves Specialist Gloves Sport Gloves
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › PrintStream.html
PrintStream (Java Platform SE 8 )
3 weeks ago - The internal error state is set to true when the underlying output stream throws an IOException other than InterruptedIOException, and when the setError method is invoked. If an operation on the underlying output stream throws an InterruptedIOException, then the PrintStream converts the exception back into an interrupt by doing:
🌐
YouTube
youtube.com › caze gaming
CUSTOM CS2 PRINTSTREAM WRAP ON LAMBO!! - YouTube
#shorts #counterstrike #cs2
Published   March 28, 2024
Views   201K
Find elsewhere
🌐
TikTok
tiktok.com › discover › printstream-cs-go-car-wrap
Printstream Cs Go Car Wrap | TikTok
2 weeks ago - Check out this custom full color change wrap with a CSGO Printstream theme for the Acura NSX! Commissions now open. #graphicdesign #Honda #Acura #CSGO #Counterstrike.
🌐
Reddit
reddit.com › r/cs2 › lamborghini huracán evo | printstream (displayed at the copenhagen major)
r/cs2 on Reddit: Lamborghini Huracán EVO | Printstream (displayed at the Copenhagen Major)
March 27, 2024 - Lamborghini | Printstream · r/cs2 • · 0:12 · upvotes · · comments · Lamborghini (StatTrak™) | Fade! r/cs2 • · upvotes · · comments · I think something broke here · r/cs2 • · 0:08 · upvotes · · comments · Just pulled this (681, MW) r/cs2 • · upvotes · · comments · What?! r/cs2 • · 0:27 · upvotes · · comments · Lamborghini Huracán EVO wrapped in PRINTSTREAM at the CPH airport during CS2 Major ·
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.io.printstream
PrintStream Class (Java.IO) | Microsoft Learn
A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the checkError method.
🌐
Android Developers
developer.android.com › api reference › printstream
PrintStream | API reference | Android Developers
February 26, 2026 - Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Baeldung
baeldung.com › home › java › java string › java printstream to string
Java PrintStream to String | Baeldung
January 5, 2024 - This article provides several ways of converting a PrintStream to a String in Java.The approaches include using ByteArrayOutputStream, a custom output stream, and Apache Commons IO.
🌐
CS2 Skins
stash.clash.gg › home › awp › printstream
AWP | Printstream - CS2 Prices (815 Listings) - CS2 Skins
Printstream
WHITE_1; BLACK_1; PEARLESCENT_1; BOLTACTION_1;
(4.4)
Price   $61.60
Top answer
1 of 1
8

Based on the source code, it looks like they discard the exception. All of the catch blocks look like this:

try {
    ...
}
catch (IOException x) {
    trouble = true; // (x is ignored)
}

So the most straightforward solution is probably to not use PrintStream, if possible.

One workaround could be to extend PrintStream and wrap the output in another OutputStream which captures the exception before PrintStream catches (and discards) it. Something like this:

package mcve.util;

import java.io.*;

public class PrintStreamEx extends PrintStream {
    public PrintStreamEx(OutputStream out) {
        super(new HelperOutputStream(out));
    }

    /**
     * @return the last IOException thrown by the output,
     *         or null if there isn't one
     */
    public IOException getLastException() {
        return ((HelperOutputStream) out).lastException;
    }

    @Override
    protected void clearError() {
        super.clearError();
        ((HelperOutputStream) out).setLastException(null);
    }

    private static class HelperOutputStream extends FilterOutputStream {
        private IOException lastException;

        private HelperOutputStream(OutputStream out) {
            super(out);
        }

        private IOException setLastException(IOException e) {
            return (lastException = e);
        }

        @Override
        public void write(int b) throws IOException {
            try {
                super.write(b);
            } catch (IOException e) {
                throw setLastException(e);
            }
        }

        @Override
        public void write(byte[] b) throws IOException {
            try {
                super.write(b);
            } catch (IOException e) {
                throw setLastException(e);
            }
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            try {
                super.write(b, off, len);
            } catch (IOException e) {
                throw setLastException(e);
            }
        }

        @Override
        public void flush() throws IOException {
            try {
                super.flush();
            } catch (IOException e) {
                throw setLastException(e);
            }
        }

        @Override
        public void close() throws IOException {
            try {
                super.close();
            } catch (IOException e) {
                throw setLastException(e);
            }
        }
    }
}
🌐
CSGOskins
csgoskins.gg › m4a1-s skins › printstream
M4A1-S | Printstream - CSGOSKINS.GG
M4A1-S | Printstream
M4A1-S | Printstream skin prices, market statistics, in-game previews, rarity levels, 3D view, exterior versions, and more.
(4.6)
Price   $167.85
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › class-use › PrintStream.html
Uses of Class java.io.PrintStream (Java Platform SE 7 )
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
🌐
Realjavaonline
realjavaonline.com › Files › printstream.php
PrintStream Class
In Other words, the flush() method is automatically invoked after a byte array is written. The PrintSream class enables you to write formatted data to underlying OutputStream. For Example, with PrintStream,int,long and other primitive data types are formatted as text rather than bytes.