Course contentsShow
← Java
Lesson 2016 of 2,87040. File I/O: Character StreamsFree lesson

PrintWriter Overview: Formatted Text Output

Understand PrintWriter's role as a character stream for writing formatted text with convenient methods.

PrintWriter Overview: Formatted Text Output

What you'll learn: You'll discover PrintWriter, Java's character stream class designed specifically for writing formatted text with convenient, easy-to-use methods.

The Convenient Text Writer

Up until now, you've used BufferedWriter for writing text and called methods like write() and newLine(). While these work perfectly, they require some manual effort when you want to write formatted output like numbers, booleans, or multiple values on one line.

PrintWriter solves this problem. It's a specialized character stream class that wraps around other Writers (or OutputStreams) and provides convenient print methods you're already familiar with from System.out.println(). Think of it as bringing the ease of console output to file writing.

Why PrintWriter Matters

PrintWriter offers methods like print(), println(), and printf() that automatically convert various data types (int, double, boolean, objects) into text format. Instead of manually converting an integer to a string before writing it, you simply pass the integer to PrintWriter and it handles the conversion.

This makes writing formatted reports, logs, or any human-readable text files much more convenient. You can write mixed data types on a single line without string concatenation gymnastics.

Key Features

  • Automatic type conversion: Write primitives and objects directly without manual toString() calls
  • Never throws checked exceptions: Unlike most I/O classes, PrintWriter methods don't throw IOException; you check errors with checkError()
  • Built-in buffering: PrintWriter buffers internally for efficiency
  • Formatted output: Supports printf-style formatting for precise control

Key Takeaway: PrintWriter is your go-to character stream for writing formatted text output, offering the same convenient print methods as System.out but targeting files or other Writers instead of the console.