Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท I/O and Files

BufferedWriter and PrintWriter

Reference for java.io.BufferedWriter and PrintWriter: write text efficiently, newLine versus println, flushing, append mode and checkError.

BufferedWriter collects characters in an in-memory buffer and writes them to the underlying Writer in larger chunks, which is much faster than writing every small piece directly to a file. PrintWriter adds the familiar print, println and printf methods; it can wrap a file, a stream or another writer.

For one-shot writes use Files.writeString. Reach for these classes when you write many pieces in a loop, or when you want printf formatting straight into a file.

Java
try (BufferedWriter out = Files.newBufferedWriter(path)) {
    out.write("first line");
    out.newLine();
}

Try it

Java BufferedWriter with newLine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java PrintWriter, printf and flushing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public class BufferedWriter extends Writer
BufferedWriter(Writer out)
BufferedWriter(Writer out, int size)

public class PrintWriter extends Writer
PrintWriter(Writer out)
PrintWriter(Writer out, boolean autoFlush)
PrintWriter(OutputStream out)    // uses the default charset
PrintWriter(String fileName, Charset cs)
Member Description
write(String), write(int c), write(char[], off, len), write(String, off, len) Write text; the int form writes one character.
newLine() (BufferedWriter) Writes the platform line separator.
append(CharSequence) Like write, but returns the writer for chaining.
flush() Pushes buffered text to the underlying writer.
close() Flushes and closes the writer, and the one it wraps.
print, println, printf, format (PrintWriter) Formatted output; printf uses %n for the line separator.
checkError() (PrintWriter) Flushes and returns true if any error has happened so far.

Throws: BufferedWriter methods throw IOException. PrintWriter methods never do: they set an internal error flag instead.

Usage notes

  • Always close (or flush). Unflushed text sits in the buffer and is lost when the program ends without close(). Try-with-resources is the safest approach.
  • write(int) writes a character, not a number: out.write(65) writes A. Use String.valueOf or PrintWriter.print for numbers.
  • println versus newLine(): both write the platform separator (\n on Linux, \r\n on Windows). To always write \n, write it yourself.
  • Silent failures. A PrintWriter hides I/O errors; call checkError() if you care, or use BufferedWriter for important data. The same applies to System.out.
  • Charset. The PrintWriter(OutputStream) and FileWriter constructors use the default charset; prefer Files.newBufferedWriter or an explicit Charset.
  • Auto-flush (the true argument) only applies to println, printf and format, and costs performance.
  • Wrapping a FileWriter is the old style: new BufferedWriter(new FileWriter("a.txt")) truncates the file unless you pass true for append.
  • Closing a wrapper closes what it wraps; do not close the inner writer separately.