Java PrintWriter Class
In Java, the PrintWriter class is part of the java.io package and provides a convenient way to write formatted text to various output destinations such as files, memory buffers, or even network streams. It supports writing text using methods like print(), println(), and printf(), much like how we use System.out, but it's important not to confuse PrintWriter with PrintStream (used by System.out).
Unlike FileWriter or BufferedWriter, PrintWriter has the additional capability to handle formatted text and can automatically flush the stream after certain write operations, if configured to do so.
1. Class Hierarchy#
PrintWriter extends Writer
2. Constructors#
autoFlush: If true, the output buffer is flushed automatically whenever aprintln,printf, orformatmethod is called.
3. Key Methods#
| Method | Description |
|---|---|
print() | Writes data without a newline |
println() | Writes data followed by a newline |
printf() | Writes formatted string (like C) |
flush() | Forces any buffered output to be written |
close() | Closes the stream |
4. Examples#
Example 1: Writing to a file#
Output (in output.txt):#
Example 2: Formatted output#
Output:#
Example 3: Using autoFlush#
5. Why Use PrintWriter?#
- Provides an easier and more readable API for writing text.
- Allows formatted output similar to
System.out.printf(). - Handles various data types automatically.
- Optional autoFlush for more control.
- Can write to files, memory buffers, sockets, etc.
6. Notes on flush() and close()#
flush()is used to push any buffered data to the destination.close()releases system resources and also flushes the stream.- If
autoFlushis enabled, you may not need to callflush()manually afterprintln()orprintf().
7. Difference Between PrintWriter and PrintStream#
While both PrintWriter and PrintStream are used for writing output and support methods like print() and println(), they are not the same. Here's a detailed comparison:
| Feature | PrintWriter | PrintStream |
|---|---|---|
| Package | java.io | java.io |
| Inheritance | Extends Writer (character stream) | Extends OutputStream (byte stream) |
| Main Use | Writing text (characters) | Writing raw bytes and text |
| Encoding Support | Yes, supports character encoding (like UTF-8) | Limited encoding support |
| Exception Handling | Does not throw IOException on writing methods (must call checkError() to detect issues) | Same behavior (silently handles exceptions unless checked) |
| Output | Works with characters (e.g., writing to a file, console with text) | More suitable for binary data or console output |
| Example | PrintWriter pw = new PrintWriter("file.txt"); | PrintStream ps = new PrintStream("file.txt"); |
| Common Usage | Writing logs, reports, or formatted text to files or console | System output like System.out and binary logs |
Important Note:#
System.outis aPrintStream, not aPrintWriter.- If you want to write human-readable formatted text with better encoding handling (like UTF-8), use
PrintWriter. - If you're dealing with console output or binary output,
PrintStreamis more appropriate.
Conclusion#
PrintWriter is a high-level, convenient Writer for printing formatted representations of objects to a text-output stream. Its similarity to System.out makes it intuitive and powerful for writing both simple and formatted data, especially in file and console output scenarios.