← Back to Week 3 Hub

FileWriter vs BufferedWriter

Same 10 lines written two ways. Watch how often each one touches the disk — and why that matters.

The big idea: FileWriter sends every write straight to disk. BufferedWriter wraps it and holds writes in an 8 KB in-memory buffer, flushing to disk only when the buffer fills or when you call close(). Fewer disk trips → faster program.
Append mode

OFF: new writes overwrite the file from the beginning. new FileWriter("out.txt").

FileWriter
no buffer
0
write() calls
0
disk trips
📄 out.txt (on disk)
BufferedWriter
8 KB buffer
0
write() calls
0
disk trips
8 KB buffer0 / 8192 B
empty
📄 out.txt (on disk)

FileWriter code

FileWriter fw = new FileWriter("out.txt");
// each write() => immediate disk trip
fw.write("Counting 1\n");
fw.write("Counting 2\n");
// ... 10 calls, 10 trips
fw.close();

BufferedWriter code

FileWriter fw = new FileWriter("out.txt");
BufferedWriter bw = new BufferedWriter(fw);
// writes collect in the 8 KB buffer
bw.write("Counting 1\n");
bw.write("Counting 2\n");
// ... buffer fills up or close() is called
bw.close(); // flushes buffer to disk

Tip: press Write 10 lines and compare disk trips. FileWriter = 10 trips. BufferedWriter = still 0 (nothing reached the disk yet). Press close() to flush the buffer.

← BufferedReader Pipeline LocalDate / LocalTime / LocalDateTime →