Java NIO & Path
In this page:
import java.nio.file.*;
Path path = Paths.get("file_name");
List<String> lines = Files.readAllLines(path);
What is NIO and Path?
The NIO (New I/O) Path interface represents a filesystem location more flexibly than the older File class, and pairs with the Files utility class for most modern file operations in Java.
Example: What is NIO and Path?
// Import java.nio.file.Path so it can be used by its short name
import java.nio.file.Path;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Path path = Path.of("data/notes.txt");
// Print a line to the console
System.out.println(path);
}
}
Login to try C/C++/Java/PHP code in the editor
Extracting Path Information
A Path object lets you extract pieces of a location — getFileName(), getParent(), getRoot() — without manually parsing separator characters yourself.
Example: Extracting Path Information
// Import java.nio.file.Path so it can be used by its short name
import java.nio.file.Path;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Path path = Path.of("data/notes.txt");
// Print a line to the console
System.out.println(path.getFileName());
// Print a line to the console
System.out.println(path.getParent());
}
}
Login to try C/C++/Java/PHP code in the editor
Absolute vs Relative Paths
An absolute path fully specifies a location from the filesystem root, while a relative path is interpreted relative to the current working directory — Path.isAbsolute() tells you which kind you're holding.
Example: Absolute vs Relative Paths
// Import java.nio.file.Path so it can be used by its short name
import java.nio.file.Path;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Path relative = Path.of("notes.txt");
Path absolute = Path.of("/home/user/notes.txt");
// Print a line to the console
System.out.println(relative.isAbsolute());
// Print a line to the console
System.out.println(absolute.isAbsolute());
}
}
Login to try C/C++/Java/PHP code in the editor
Resolving Paths
resolve() combines a base path with a relative path segment to build a new, longer path, which is the standard NIO way to construct file locations piece by piece.
Example: Resolving Paths
import java.nio.file.Path;
public class Main {
public static void main(String[] args) {
Path base = Path.of("data");
Path full = base.resolve("notes.txt"); // combines pieces
System.out.println(full);
}
}
Login to try C/C++/Java/PHP code in the editor
Normalizing Paths
normalize() cleans up redundant . and .. segments in a path without touching the actual filesystem, giving you a canonical, simplified representation for comparison or display.
Example: Normalizing Paths
// Import java.nio.file.Path so it can be used by its short name
import java.nio.file.Path;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Path messy = Path.of("data/../data/./notes.txt");
// Print a line to the console
System.out.println(messy.normalize());
}
}
Login to try C/C++/Java/PHP code in the editor
- Confusing
java.io.Filewithjava.nio.file.Path. - Writing Windows paths such as
"C:\Users"with single backslashes, which are treated as escape sequences in Java strings. - Expecting
normalize()to check that the path exists, when it only cleans.and..from the path text.
- Java I/O streams read and write data, and you can create and delete files.
FileReader,FileWriter, buffered readers and writers, andFileInputStreamandFileOutputStreamhandle text and byte data.- Serialization saves objects, and NIO and
Pathoffer a modern way to work with files.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: