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.
try (BufferedWriter out = Files.newBufferedWriter(path)) { out.write("first line"); out.newLine(); }
Try it
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.
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
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)writesA. UseString.valueOforPrintWriter.printfor numbers.printlnversusnewLine(): both write the platform separator (\non Linux,\r\non Windows). To always write\n, write it yourself.- Silent failures. A
PrintWriterhides I/O errors; callcheckError()if you care, or useBufferedWriterfor important data. The same applies toSystem.out. - Charset. The
PrintWriter(OutputStream)andFileWriterconstructors use the default charset; preferFiles.newBufferedWriteror an explicitCharset. - Auto-flush (the
trueargument) only applies toprintln,printfandformat, and costs performance. - Wrapping a
FileWriteris the old style:new BufferedWriter(new FileWriter("a.txt"))truncates the file unless you passtruefor append. - Closing a wrapper closes what it wraps; do not close the inner writer separately.
Related
BufferedReader,Files.writeString(),OutputStream, Charsets- Lessons: I/O streams, Files