← Back to Core Java Course | Chapter 12: File I/O | Lesson 9 of 9

Java NIO & Path

NIO and Path give a modern way to describe where a file lives, like writing an address for a file. You can join, clean up, and compare these addresses.
Syntax
java
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?

java
// 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);
	}
}

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

java
// 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());
	}
}

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

java
// 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());
	}
}

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

java
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);
	}
}

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

java
// 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());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Confusing java.io.File with java.nio.file.Path.
  2. Writing Windows paths such as "C:\Users" with single backslashes, which are treated as escape sequences in Java strings.
  3. Expecting normalize() to check that the path exists, when it only cleans . and .. from the path text.
Chapter Summary
  • Java I/O streams read and write data, and you can create and delete files.
  • FileReader, FileWriter, buffered readers and writers, and FileInputStream and FileOutputStream handle text and byte data.
  • Serialization saves objects, and NIO and Path offer a modern way to work with files.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.