Java BufferedReader & BufferedWriter
In this page:
Writing Lines with BufferedWriter
BufferedWriter wraps another writer (like FileWriter) and batches output in memory, flushing to disk in larger chunks instead of on every single write call — this is significantly faster for writing many lines.
Example: Writing Lines with BufferedWriter
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter("notes.txt"));
writer.write("Line one");
writer.newLine();
writer.write("Line two");
writer.close();
System.out.println("Written");
}
}
Login to try C/C++/Java/PHP code in the editor
Reading Lines with BufferedReader
BufferedReader wraps another reader and adds readLine(), which returns a full line of text at once (or null at end-of-file) — far more convenient than reading one character at a time.
Example: Reading Lines with BufferedReader
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter writer = new FileWriter("notes.txt");
writer.write("Hello\nWorld");
writer.close();
BufferedReader reader = new BufferedReader(new FileReader("notes.txt"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
}
Login to try C/C++/Java/PHP code in the editor
Flushing Buffered Streams
Buffered streams don't write to disk immediately; calling flush() forces any buffered data out now, and closing the stream flushes automatically, so unflushed data is only truly lost if the program crashes before closing.
Example: Flushing Buffered Streams
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter("notes.txt"));
writer.write("Buffered data");
writer.flush(); // forces it out now
writer.close();
System.out.println("Flushed and closed");
}
}
Login to try C/C++/Java/PHP code in the editor
Exception Handling
File I/O can throw checked IOExceptions at almost any point — a missing file, a full disk, a permissions error — so buffered reads and writes need the same try-catch or try-with-resources discipline as unbuffered ones.
Example: Exception Handling
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("missing.txt"))) {
System.out.println(reader.readLine());
} catch (IOException e) {
System.out.println("Caught: " + e.getClass().getSimpleName());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Data Aggregation
Reading a whole file line-by-line into a List<String> or building a running total from numeric lines are typical uses for BufferedReader, since readLine() naturally breaks input into processable chunks.
Example: Data Aggregation
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter writer = new FileWriter("numbers.txt");
writer.write("10\n20\n30");
writer.close();
BufferedReader reader = new BufferedReader(new FileReader("numbers.txt"));
int total = 0;
String line;
while ((line = reader.readLine()) != null) {
total += Integer.parseInt(line);
}
reader.close();
System.out.println(total);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: