Sub topic 8
Buffered Readers and Writers
Assume you know basic Java classes, checked exceptions, and try-with-resources. This chapter uses Java 11 or later.
When a program reads or writes a file, it communicates with the operating system. Each communication can be relatively expensive. A buffer is a temporary area in memory that collects data so the program can perform fewer, larger operations.
Java provides these buffered classes:
| Class | Purpose |
|---|---|
| `BufferedReader` | Reads characters efficiently, often one line at a time |
| `BufferedWriter` | Writes characters efficiently, often one line at a time |
Use Files.newBufferedReader and Files.newBufferedWriter to create them from a Path.
Reading and writing line by line
The following program creates an input file, reads it one line at a time, converts each line to uppercase, and writes the result to another file.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
public class CopyUpperCase {
public static void main(String[] args) throws IOException {
Path input = Path.of("input.txt");
Path output = Path.of("output.txt");
Files.writeString(input, "first line\nsecond line\n",
StandardCharsets.UTF_8);
try (BufferedReader reader =
Files.newBufferedReader(input, StandardCharsets.UTF_8);
BufferedWriter writer =
Files.newBufferedWriter(output, StandardCharsets.UTF_8)) {
String line;
while ((line = reader.readLine()) != null) {
writer.write(line.toUpperCase(Locale.ROOT));
writer.newLine();
}
}
System.out.println(Files.readString(output));
}
}readLine() returns the next line without its line-ending characters. It returns null when there are no more lines. The loop therefore continues until the end of the file.
write() writes characters but does not add a line ending. Use newLine() when the output should preserve line boundaries. This method uses the line-ending convention of the current operating system.
The charset matters. StandardCharsets.UTF_8 tells Java how to convert bytes in the file into characters and back again. Always choosing a charset makes the program's behavior predictable across machines.
Why try-with-resources matters
Both objects hold operating-system resources. The try block automatically closes them, even when an exception occurs. Closing the writer also flushes characters still waiting in its buffer. Without closing or flushing it, the output file may be incomplete.
A reader can throw NoSuchFileException when the input path does not exist. Other failures, such as permission errors, are reported through IOException.
The data flow
For a very large file, this approach keeps only the current line and buffer contents in memory. It is usually safer than loading the entire file with Files.readString or Files.readAllLines. A single extremely long line can still require substantial memory.
Exercise
Why does the example call writer.newLine() after writer.write(...)?
Show answer
write() writes only the characters it receives. newLine() adds the platform-appropriate line ending so each input line remains a separate output line.