java.io and java.nio.file
Read, write, and navigate files with java.io and java.nio.file: Path, Files, BufferedReader, BufferedWriter, and directory walking.
Read, write, and navigate files with java.io and java.nio.file: Path, Files, BufferedReader, BufferedWriter, and directory walking.
Introduction
Java provides two main packages for file and stream I/O: java.io (stream-oriented) and java.nio.file (added in Java 7, with Path, channel, and filesystem APIs). The java.nio.file package, often called NIO.2, is the modern choice for most file operations. The legacy java.io package remains useful for character streams and interoperability with older libraries.
Reading a small text file into a String with Files.readString() is one line; using the same approach on a very large file can exhaust memory. Untrusted paths also need care: .. segments and symbolic links can escape an allowed directory, and concurrent changes to path components can create a check-to-use race.
This guide covers the architecture of both packages, common NIO.2 operations, path manipulation with Path, directory walking with Files.walk(), and common failure scenarios. It also explains how to handle untrusted paths that may contain traversal segments or symbolic links.
When to Use
| Task | Recommended API |
|---|---|
| Simple file read/write | Files.readString(), Files.writeString() (Java 11+) |
| Reading lines from a file | Files.readAllLines(), BufferedReader.lines() |
| Buffered binary read/write | BufferedInputStream / BufferedOutputStream |
| Buffered character read/write | BufferedReader / BufferedWriter |
| Walking a directory tree | Files.walk() |
| Path manipulation | java.nio.file.Path |
| Stream-based processing | java.nio.file.Files.lines() |
| File metadata and attributes | Files.readAttributes(), Files.getLastModifiedTime() |
When NOT to Use
- New code requiring cross-platform file locking: Use
FileChannelwith explicit locking rather thanFile.canWrite()/canRead()checks that are not atomic. - Assuming zero-copy for binary transfers:
FileChannel.transferTo()andtransferFrom()may be optimized by some operating systems for fast transfers, but zero-copy is not guaranteed. The result depends on the source and target channels, operating system, filesystem, and transfer path; measure the path your application uses. - Unbounded file reading: Never use
readAllBytes()on potentially large files (GB scale) — useFiles.lines()with try-with-resources and a stream approach. - Path string construction: Avoid string concatenation for paths — use
Path.resolve()instead to handle edge cases like double slashes.
Architecture
flowchart TD
subgraph java.io
A1[InputStream / OutputStream]
A2[Reader / Writer]
A3[FileInputStream / FileOutputStream]
A4[BufferedReader / BufferedWriter]
end
subgraph java.nio.file
B1[Path]
B2[Files]
B3[FileSystem]
B4[DirectoryStream]
end
subgraph java.nio.channels
C1[ByteChannel]
C2[FileChannel]
C3[SeekableByteChannel]
end
B1 --> B2
B2 --> C2
B2 --> B4
style java.nio.file fill:#1a1a2e,stroke:#00fff9,color:#00fff9
style java.io fill:#0d0d1a,stroke:#ff00ff,color:#fff
Code Examples
java.nio.file — Modern File Operations
The java.nio.file package (NIO.2, Java 7) handles file and directory operations. It fixes the problems with java.io.File, which jammed file, directory, and path concepts together and had no support for symbolic links or fine-grained attribute access. NIO.2 splits these up: Path is a file or directory location, Files has static utility methods for file operations, and FileSystem wraps the underlying filesystem.
The Files utility class is where most operations start. It gives you readString(Path), writeString(Path, CharSequence, OpenOption...), readAllLines(Path), copy(Path, Path, CopyOption...), move(Path, Path, MoveOption...), and more. All of these take Path objects instead of strings, which makes path construction composable and correct. StandardOpenOption controls file creation, truncation, and append behavior. Need atomic check-and-create, like making a lock file that must not already exist? Use StandardOpenOption.CREATE_NEW.
import java.nio.file.*;
import java.io.IOException;
Path path = Path.of("/tmp/data.txt");
// Read entire file (Java 11+)
String content = Files.readString(path);
// Write file (Java 11+)
Files.writeString(path, "Hello, World!", StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
// Read all lines
List<String> lines = Files.readAllLines(path);
// Write lines
Files.write(path, List.of("Line 1", "Line 2"));
// Check existence and permissions
if (Files.exists(path) && Files.isReadable(path)) {
// ...
}
// Copy and move
Files.copy(Path.of("/tmp/source.txt"), Path.of("/tmp/dest.txt"), StandardCopyOption.REPLACE_EXISTING);
Files.move(Path.of("/tmp/src"), Path.of("/tmp/dst"), StandardCopyOption.ATOMIC_MOVE);
// Temp files
Path tempFile = Files.createTempFile("prefix", ".txt");
Path tempDir = Files.createTempDirectory("prefix");
Streaming Lines
Files.lines() reads lazily and keeps the file open while the stream is in use. Close it with try-with-resources:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
public class FilesLinesExample {
public static void main(String[] args) throws IOException {
Path path = Path.of(args[0]);
try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
lines.filter(line -> line.contains("ERROR"))
.forEach(System.out::println);
}
}
}
For example, if app.log contains these lines, run javac FilesLinesExample.java && java FilesLinesExample app.log:
INFO ready
ERROR database unavailable
The program prints only the matching line:
ERROR database unavailable
Path Manipulation
Path is an immutable file or directory location. Unlike a string, Path knows the filesystem’s path separator, handles normalization of . and .. segments, and provides compositional methods for building paths from parts. Path.of(String) (Java 11+) or the older Paths.get(String) creates a Path from a string. One rule: never concatenate strings to build paths. base + "/" + name breaks on Windows (which uses \) and misses edge cases like double slashes. Use base.resolve(name).
resolve() adds a path segment to an existing path. relativize() finds the relative path between two absolute paths. normalize() removes redundant . and .. segments lexically; it does not access the filesystem or resolve symbolic links. That makes it useful for an initial containment check, but not enough on its own for an untrusted path. subpath(int begin, int end) pulls out a portion of the path for tree-walking or display. toAbsolutePath() converts a relative path to absolute by resolving it against the current working directory.
import java.nio.file.Path;
Path base = Path.of("/home/user/projects");
Path file = Path.of("/home/user/projects/src/main/java/App.java");
// Resolve — append path segments
Path joined = base.resolve("src/config.yaml"); // /home/user/projects/src/config.yaml
// Relativize — find relative path between two absolutes
Path relative = base.relativize(file); // src/main/java/App.java
// Normalize — collapse . and .. lexically
Path messy = Path.of("/home/user/../user/./projects/./app");
Path normalized = messy.normalize(); // /home/user/projects/app
// Subpath
Path sub = file.subpath(1, 3); // projects/src
// Get parts
file.getFileName(); // App.java
file.getParent(); // /home/user/projects/src/main/java
file.getRoot(); // /
Directory Walking
Files.walk(Path) returns a lazy Stream<Path> that traverses a directory tree depth-first. This replaces the manual recursive traversal code you had to write before NIO.2. The stream is lazy, so it only reads directory entries as you consume them, making it memory-efficient for large trees. Use it in a try-with-resources block because it holds a directory handle open until the stream closes or fully consumes.
By default, Files.walk() does not follow symbolic links. To follow them, pass FileVisitOption.FOLLOW_LINKS, for example Files.walk(root, maxDepth, FileVisitOption.FOLLOW_LINKS). When following links, the walk detects cycles and can report a FileSystemLoopException. Keep the default when links are not needed, and use a maximum depth when the tree may be large. For a single-level listing with a glob, Files.newDirectoryStream(Path, String) avoids a recursive walk.
import java.nio.file.*;
import java.util.stream.*;
import java.io.IOException;
// Walk entire tree
try (Stream<Path> stream = Files.walk(Path.of("/tmp/myproject"))) {
stream.filter(Files::isRegularFile)
.filter(p -> p.toString().endsWith(".java"))
.forEach(System.out::println);
}
// Walk with max depth
try (Stream<Path> stream = Files.walk(Path.of("/tmp"), 2)) {
// Only visits depth 0, 1, 2
}
// Follow links only when this behavior is intentional
try (Stream<Path> stream = Files.walk(
Path.of("/tmp/myproject"), 4, FileVisitOption.FOLLOW_LINKS)) {
stream.forEach(System.out::println);
}
// Find files with specific glob
try (DirectoryStream<Path> ds = Files.newDirectoryStream(Path.of("/tmp"), "*.txt")) {
for (Path p : ds) {
System.out.println(p);
}
}
// List directory contents
try (DirectoryStream<Path> ds = Files.newDirectoryStream(Path.of("/tmp"))) {
ds.forEach(System.out::println);
}
Buffered Character Streams (java.io)
BufferedReader and BufferedWriter wrap character streams with buffering, which speeds up character-by-character or line-by-line processing. Without buffering, each read() or write() call translates to a native IO operation, which is expensive. A BufferedReader reads ahead into an in-memory buffer, so subsequent read() calls pull from the buffer without hitting the OS. BufferedWriter buffers writes before flushing them to the underlying Writer in larger chunks.
BufferedReader.lines() (Java 8+) returns a Stream<String> of all lines in the file, lazy and one line at a time, without loading the entire file into memory. This is the right approach for processing large files line by line. The transferTo() method (Java 10+) on BufferedReader transfers the entire stream content directly to a Writer without intermediate buffering at the Java level, which is efficient for copying. Always specify the charset explicitly when constructing character streams. Wrapping FileReader directly uses the platform default encoding, which varies across systems and JVM configurations.
import java.io.*;
import java.nio.file.Path;
Path logFile = Path.of("/tmp/app.log");
// BufferedReader — reading lines
try (BufferedReader reader = new BufferedReader(new FileReader(logFile.toFile()))) {
reader.lines()
.filter(line -> line.contains("ERROR"))
.forEach(System.out::println);
}
// BufferedWriter — writing
try (BufferedWriter writer = new BufferedWriter(new FileWriter(logFile.toFile()))) {
writer.write("Application started at " + java.time.LocalDateTime.now());
writer.newLine();
writer.flush();
}
// Try-with-resources for multi-stream handling
try (BufferedReader reader = new BufferedReader(new FileReader("/tmp/in.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("/tmp/out.txt"))) {
reader.transferTo(writer); // Java 9+
}
File Attributes
Files.readAttributes(Path, Class<A>) reads filesystem-agnostic basic file attributes: size, creation time, last modified time, and file type (regular file, directory, symbolic link). The BasicFileAttributes interface gives you size() for file size in bytes, creationTime() returning a FileTime, lastModifiedTime() for the modification timestamp, and isRegularFile() / isDirectory() / isSymbolicLink() type checks. These attributes work across all major filesystems and are the correct choice for cross-platform attribute access.
On Unix-like systems (Linux, macOS), Files.readAttributes(path, PosixFileAttributes.class) provides POSIX attributes: owner, group, and permission bits, the same information you see from ls -l. On Windows, use DosFileAttributes. FileTime values from attribute reads convert to java.time.Instant via fileTime.toInstant() for interoperability with the java.time API. To set file times, use Files.setLastModifiedTime(path, FileTime.fromMillis(millis)). Files.getFileStore(path) tells you which physical or logical volume a file lives on, useful for detecting cross-filesystem moves.
import java.nio.file.*;
import java.io.IOException;
import java.nio.file.attribute.*;
Path file = Path.of("/tmp/data.txt");
// Basic attributes
BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
attrs.size(); // file size in bytes
attrs.creationTime(); // creation time
attrs.lastModifiedTime(); // last modified time
attrs.isRegularFile(); // true
attrs.isDirectory(); // true/false
attrs.isSymbolicLink(); // true/false
// POSIX attributes (Unix)
PosixFileAttributes posixAttrs = Files.readAttributes(file, PosixFileAttributes.class);
posixAttrs.owner(); // OwnerPrincipal
posixAttrs.group(); // GroupPrincipal
posixAttrs.permissions(); // POSIX permissions set
// Set last modified time
Files.setLastModifiedTime(file, FileTime.fromMillis(System.currentTimeMillis()));
Failure Scenarios
| Scenario | Problem | Solution |
|---|---|---|
Files.readString() on large file |
OutOfMemoryError |
Stream via Files.lines() in try-with-resources or use BufferedReader |
| Race condition on file existence check | TOCTOU vulnerability | Use Files.createFile() with StandardOpenOption.CREATE_NEW to atomically check-and-create |
Symbolic links during walk() |
Traversal leaves the intended tree if links are followed | Files.walk() does not follow links by default; set a depth limit and add FOLLOW_LINKS only when needed |
Files.move() across filesystem boundaries |
AtomicMoveNotSupportedException |
Copy then delete, or use REPLACE_EXISTING with copy |
| Encoding issues with FileReader | Uses platform default encoding | Always specify StandardCharsets.UTF_8 explicitly |
Trade-off Table
| Aspect | java.io |
java.nio.file |
|---|---|---|
| API design | Stream-oriented (byte/char) | Buffer-oriented with Path abstraction |
| Performance | Good for small files | Channels and buffers may help large transfers; measure the actual workload |
| Directory operations | Manual recursion | Files.walk() built-in |
| Symbolic links | Limited support | Full support via Path and LinkOption |
| Non-blocking IO | Not supported | Supported via AsynchronousFileChannel |
| Modern code preference | Legacy interop | New code |
Observability Checklist
// Instrument file operations
import java.nio.file.*;
import java.time.Instant;
public class InstrumentedFile {
public static String readFile(Path path) throws IOException {
long start = System.nanoTime();
try {
String content = Files.readString(path);
System.out.println("metric=file_read path=" + path +
" size=" + content.length() +
" duration_ns=" + (System.nanoTime() - start));
return content;
} catch (IOException e) {
System.out.println("metric=file_read path=" + path + " error=true");
throw e;
}
}
}
- Track file read/write latency as structured metrics.
- Monitor file sizes at ingestion time to detect anomalous payloads.
- Log
IOExceptionwith the failing path and root cause (but not stack traces to untrusted callers). - Use
Files.walk()with max depth to prevent accidental traversal into enormous directory trees. - Instrument directory walk completion with number of files visited and total size.
Security Notes
- Path traversal: Resolve an untrusted relative path under a trusted root, normalize it, and check containment with
Path.startsWith(). This is a path-component check, but it is only lexical: it does not resolve symbolic links. - Symbolic links: For an existing target, resolve both the allowed root and candidate with
toRealPath(), then check that the real target still starts with the real root.toRealPath()requires the path to exist. For a new file, check its existing parent directory and create the file beneath that directory. - Check-to-use races: A real-path check can race if an attacker can change path components after validation but before the file is opened. Keep untrusted users from changing those directories; where that is not possible, use a filesystem-specific secure-open approach that anchors operations to an already-open directory.
- Directory walking:
Files.walk()does not follow symbolic links unlessFileVisitOption.FOLLOW_LINKSis supplied.LinkOption.NOFOLLOW_LINKSbelongs only on APIs that accept aLinkOption; it is not an argument toFiles.walk(). - Temporary file creation: Always use
Files.createTempFile()with proper permissions — avoid race conditions in temp file naming.
For an existing file, a check can look like this:
import java.io.IOException;
import java.nio.file.Path;
public class ExistingPathUnderRoot {
static Path resolveExisting(Path allowedRoot, String userInput) throws IOException {
Path root = allowedRoot.toRealPath();
Path relative = Path.of(userInput);
if (relative.isAbsolute()) {
throw new SecurityException("Path must be relative");
}
Path candidate = root.resolve(relative).normalize();
if (!candidate.startsWith(root)) {
throw new SecurityException("Path escapes the allowed root");
}
Path realTarget = candidate.toRealPath();
if (!realTarget.startsWith(root)) {
throw new SecurityException("Path resolves outside the allowed root");
}
return realTarget;
}
}
This does not make a later open race-free if another process can replace a checked path component.
Pitfalls
Path.of()vsPaths.get():Path.of()(Java 11+) is the preferred static factory;Paths.get()is the older form. Both work, but preferPath.of()for consistency.Files.readAllBytes()on huge files: This loads the entire file into memory — for files that could be multi-GB, useFiles.lines()with a stream orBufferedReaderline-by-line.- FileReader encoding:
new FileReader(file)uses the platform default encoding, which varies by OS. Always wrap withnew InputStreamReader(Files.newInputStream(file), StandardCharsets.UTF_8). Files.walk()holds resources: The stream returned byFiles.walk()must be used within a try-with-resources block, as it holds a directory handle open until the stream is closed.transferTo()in BufferedReader: ThetransferTo()method onBufferedReader(Java 10+) transfers directly without explicit encoding handling — ensure the underlying streams use compatible encodings.
Quick Recap
- Use
java.nio.file(Files,Path) for all new file operations. Files.readString()/Files.writeString()(Java 11+) for simple text file operations.Files.walk()for directory tree traversal — always use in try-with-resources.Path.resolve()to build paths safely; never concatenate strings.Path.normalize()for a lexical cleanup and initial containment check; it does not resolve symbolic links.- Use
BufferedReader.lines()for line-by-line stream processing of large files. - Close streams from both
Files.walk()andFiles.lines()with try-with-resources. Files.walk()does not follow links by default; passFileVisitOption.FOLLOW_LINKSonly when following them is intentional.- Specify charset explicitly when wrapping character streams.
- Use
StandardOpenOption.CREATE_NEWfor atomic check-and-create operations.
Interview Questions
Further Reading
- Java SE 27
FilesAPI — directory walking, symbolic-link options, and resource-closing behavior forFiles.walk()andFiles.lines() - Java SE 27
FileChannelAPI — file transfer methods and platform-dependent optimization - Java SE 27
StreamAPI — closing streams backed by I/O resources - OWASP Path Traversal Prevention Cheat Sheet — containment checks and symlink considerations
- Oracle: File I/O (featuring NIO.2) — official Java tutorial on NIO.2 file operations
- Baeldung: Java Files API Guide — practical coverage of
java.nio.fileutilities - NIO.2 Path API vs legacy File API — migration patterns from
java.io.Filetojava.nio.file.Path - Java 11 Files.writeString source code — read the actual implementation to understand the guarantees and edge cases
- Text Formatting — text handling and formatting companion topic
- Java Collections Utility — utility methods for collections that overlap with file processing patterns
Conclusion
Java’s file IO story improved significantly in Java 7 with java.nio.file, which introduced the Path abstraction, rich file utility methods, and directory walking capabilities. For most new code, java.nio.file is the right choice — Files.readString() and Files.writeString() (Java 11+) handle simple text file operations cleanly, while Files.walk() replaces manual recursive directory traversal with a lazy stream-based approach.
The Path abstraction avoids string-based path joining. Path.resolve() appends path segments, Path.normalize() removes . and .. lexically, and Path.relativize() computes a relative path between two paths. For untrusted input, normalize and check containment, then resolve an existing candidate with toRealPath() to account for symbolic links. A separate check can still race if another process can change the path before it is opened.
Files.walk() returns a stream that holds directory resources open until it is closed, so use try-with-resources. It does not follow symbolic links by default; pass FileVisitOption.FOLLOW_LINKS only when that behavior is needed. A depth limit can bound a large traversal. Files.lines() also retains an open file and should be closed with try-with-resources.
For large file processing, Files.readAllBytes() and Files.readAllLines() load the whole file into memory. Use Files.lines() or BufferedReader for line-by-line processing, and close the stream or reader when finished.
File operations integrate with java.util.Collections when you need to collect file listing results, and java.util.stream.Stream for processing file contents as a stream of lines.
- Use
java.nio.file(Files,Path) for all new file operations — avoidjava.iofor new code Files.readString()/Files.writeString()(Java 11+) for simple text filesFiles.walk()must be used in try-with-resources — it holds an open directory handle- Use
Path.resolve()for path construction — never concatenate strings for paths - For large files, use streaming approaches (
Files.lines(),BufferedReader) — never load entire file into memory Files.walk()does not follow links by default; useFileVisitOption.FOLLOW_LINKSonly when needed and set a depth limit when appropriate
Category
Related Posts
Abstract Classes in Java: Interfaces and Composition
Compare Java interfaces, abstract classes, and composition, then see when shared state, default methods, or sealed types change the design choice.
Arithmetic Operators in Java: Division, Overflow, Promotion
Compare Java arithmetic with runnable examples of division, negative remainders, overflow, type promotion, and BigDecimal for decimal money.
Java Array Basics: Length, Bounds, Copies, and Resizing
Learn Java arrays through runtime-sized allocation, bounds checks, aliasing, copying, and the fixed length that distinguishes them from ArrayList.