Reading & Writing Files
Knowing a file's path (via java.io.File, from the previous page) is only half the story — actually reading or writing its contents needs a stream. Java offers both a classic, verbose approach and much simpler modern one-liners.
Reading a File: Classic Approach
The traditional way to read a text file line by line wraps a FileReader in a BufferedReader, which adds an efficient readLine() method.
Reading with BufferedReader
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ClassicReadDemo {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("notes.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Could not read file: " + e.getMessage());
}
}
}Reading a File: Modern One-Liners
java.nio.file.Files offers the same result with far less ceremony: Files.readAllLines(path) (Java 8+) returns every line as a List<String>, and Files.readString(path) (Java 11+) returns the entire file as a single String.
Reading with Files.readString / Files.readAllLines
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.IOException;
import java.util.List;
public class ModernReadDemo {
public static void main(String[] args) throws IOException {
Path path = Path.of("notes.txt");
String wholeFile = Files.readString(path); // Java 11+
System.out.println(wholeFile);
List<String> lines = Files.readAllLines(path); // Java 8+
System.out.println("Line count: " + lines.size());
}
}Writing a File: Classic vs. Modern
Writing with FileWriter/BufferedWriter
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class ClassicWriteDemo {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write("Hello, file!");
writer.newLine();
writer.write("Second line.");
} catch (IOException e) {
System.out.println("Could not write file: " + e.getMessage());
}
}
}Writing with Files.writeString
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.IOException;
public class ModernWriteDemo {
public static void main(String[] args) throws IOException {
Path path = Path.of("output.txt");
Files.writeString(path, "Hello, file!" + System.lineSeparator() + "Second line.");
}
}throws IOException on the enclosing method. There is no way to silently ignore the possibility of a file operation failing.BufferedReader/FileReaderandFileWriter/BufferedWriterare the classic, stream-based approach.Files.readString()/Files.readAllLines()(reading) andFiles.writeString()(writing) are much simpler modern one-liners.Always wrap classic stream resources in try-with-resources so they are closed automatically.
IOExceptionis checked — every file operation forces you to catch it or declarethrows IOException.