Part 07 / Topic 4 of 7
Reading and writing text files with java.nio.file
java.nio.file ("new I/O", despite the name — it has been the standard way to touch files since Java 7) gives you a Path (a file or folder location, not yet opened) and a Files class full of static helper methods that do the actual reading and writing.
Think of Path as a street address written on paper, and Files as the courier: the address alone delivers nothing until a courier method acts on it.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
public class FileBasics {
public static void main(String[] args) throws IOException {
Path dataDir = Path.of("data");
Files.createDirectories(dataDir);
Path notePath = dataDir.resolve("note.txt");
Files.writeString(notePath, "Shopping list for the Pune store visit\n");
System.out.println("Exists after write: " + Files.exists(notePath));
Files.writeString(notePath, "- Rice 5kg\n- Dal 2kg\n", StandardOpenOption.APPEND);
String wholeFile = Files.readString(notePath);
System.out.println("---- whole file (Files.readString) ----");
System.out.print(wholeFile);
List<String> lines = Files.readAllLines(notePath);
System.out.println("---- line count (Files.readAllLines) ----");
System.out.println(lines.size());
List<String> extra = List.of("- Ghee 1kg", "- Atta 10kg");
Files.write(notePath, extra, StandardOpenOption.APPEND);
System.out.println("---- after appending a list (Files.write) ----");
for (String line : Files.readAllLines(notePath)) {
System.out.println(line);
}
}
}
Output:
Exists after write: true
---- whole file (Files.readString) ----
Shopping list for the Pune store visit
- Rice 5kg
- Dal 2kg
---- line count (Files.readAllLines) ----
3
---- after appending a list (Files.write) ----
Shopping list for the Pune store visit
- Rice 5kg
- Dal 2kg
- Ghee 1kg
- Atta 10kg
And the real file left behind on disk:
$ cat data/note.txt
Shopping list for the Pune store visit
- Rice 5kg
- Dal 2kg
- Ghee 1kg
- Atta 10kg
| Method | Does |
|---|---|
Path.of("data", "note.txt") / dir.resolve("note.txt") |
build a path, without touching the disk |
Files.createDirectories(path) |
make a folder (and any missing parent folders); does nothing if it already exists |
Files.exists(path) |
true/false, no exception either way |
Files.writeString(path, text) |
write a whole String, overwriting the file (creates it if missing) |
Files.writeString(path, text, StandardOpenOption.APPEND) |
add text to the end of an existing file |
Files.readString(path) |
read the whole file as one String |
Files.readAllLines(path) |
read the whole file as a List<String>, one entry per line |
Files.write(path, List<String>) |
write a list of lines, one per line, overwriting the file |
All of these throw a checked IOException (Part 6) if the disk operation fails — a missing folder, no permission, a full disk. Files.writeString/readString/readAllLines/write read and write as UTF-8 by default, so ₹ and other non-ASCII characters round-trip correctly without any extra setup.
These whole-file methods are simple and fine for small files (configuration, a to-do list, a short log). For very large files, reading everything into one String or List at once wastes memory — Chapter 5 shows the line-by-line alternative.
Full program: examples/ex02-nio-file-basics/FileBasics.java.
Done here? The practice set for Part 7 — including the solutions — is on the part page.