Java Programming Handbook

    Java PrintStream Class

    The PrintStream class in Java is a specialized output stream used to print data in a human-readable format. It extends the FilterOutputStream class and adds functionality to write formatted representations of objects to the output stream.

    1. Why Use PrintStream?#

    While FileOutputStream and BufferedOutputStream work well for raw byte operations, PrintStream is ideal for:

    • Printing textual representations of data
    • Automatically flushing output
    • Writing formatted output without needing manual conversions

    You might already be familiar with it — System.out is a PrintStream object!

    2. Creating a PrintStream Object#

    You can create a PrintStream using various constructors:

    PrintStream ps = new PrintStream(String fileName); PrintStream ps = new PrintStream(File file); PrintStream ps = new PrintStream(OutputStream out);

    3. Key Methods in PrintStream (With Examples)#

    void print(String s)#

    Prints a string to the stream.

    import java.io.*; public class PrintStreamPrintExample { public static void main(String[] args) throws FileNotFoundException { // Creating a PrintStream linked to output.txt PrintStream ps = new PrintStream("output.txt"); // Writing plain text to the file ps.print("Hello from PrintStream!"); ps.close(); } }

    Output (content inside output.txt):#

    Hello from PrintStream!

    void println(String s)#

    Prints a string followed by a newline character.

    import java.io.*; public class PrintStreamPrintlnExample { public static void main(String[] args) throws FileNotFoundException { PrintStream ps = new PrintStream("output.txt"); // Printing lines with newline ps.println("Line 1"); ps.println("Line 2"); ps.close(); } }

    Output:#

    Line 1 Line 2

    void printf(String format, Object... args)#

    Prints a formatted string using the specified format and arguments.

    import java.io.*; public class PrintStreamPrintfExample { public static void main(String[] args) throws FileNotFoundException { PrintStream ps = new PrintStream("output.txt"); // Writing formatted text ps.printf("Name: %s, Age: %d", "Alice", 25); ps.close(); } }

    Output:#

    Name: Alice, Age: 25

    void write(int b)#

    Writes the specified byte to the stream. Useful for writing binary data.

    import java.io.*; public class PrintStreamWriteExample { public static void main(String[] args) throws FileNotFoundException { PrintStream ps = new PrintStream("output.txt"); // Writes character 'A' (ASCII value 65) ps.write(65); ps.close(); } }

    Output:#

    A

    void flush()#

    Forces any buffered output bytes to be written out. Usually not needed when using println, but good to know.

    import java.io.*; public class PrintStreamFlushExample { public static void main(String[] args) throws FileNotFoundException { PrintStream ps = new PrintStream("output.txt"); ps.print("Data not yet flushed"); // Manually flush to ensure data is written immediately ps.flush(); ps.close(); } }

    void close()#

    Closes the stream and releases any system resources associated with it.

    import java.io.*; public class PrintStreamCloseExample { public static void main(String[] args) throws FileNotFoundException { PrintStream ps = new PrintStream("output.txt"); ps.print("Closing the stream now."); ps.close(); // Recommended to close to release file resources } }

    4. Special Note: System.out is a PrintStream#

    You use it every day without realizing:

    System.out.println("This is a PrintStream!");

    It behaves just like the examples shown above.

    5. When Should You Use PrintStream?#

    • When you want to print nicely formatted, human-readable output
    • When writing data to files and you want to use print/println/printf features
    • When you don’t need to worry about character encoding or internationalization (for that, use PrintWriter)

    Conclusion#

    In this blog, we learned about the PrintStream class, which is commonly used for writing formatted and human-readable output in Java. We explored its key methods like print, println, printf, write, flush, and close with practical examples. This class is extremely useful for logging and writing text data to files.

    Last updated on Apr 09, 2025