Files & Paths in Java

From the Java cheat sheet · File I/O · verified Jul 2026

Files & Paths

Modern file reading and writing with java.nio.

java
// Quick Reference
Path p = Path.of("data.txt");
String text = Files.readString(p);
Files.writeString(p, "hello");
List<String> lines = Files.readAllLines(p);
💡 Files.readString / writeString (Java 11+) handle small files in a single call.
⚡ For large files use Files.lines() in try-with-resources so the stream closes.
📌 Path.of(...) is the modern replacement for the older new File(...) API.
🟢 Files I/O throws IOException (checked) - handle or declare it with throws.
file-ionio
Back to the full Java cheat sheet