Java Academy · Lesson

Path: Representing File Locations

Create Path objects, resolve and relativize paths, and manipulate path components.

Lesson 1 of 413 steps

Path: Representing File Locations is a free Java Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, and your progress syncs across the web and the CoddyKit app. The Java Academy course includes 4 lessons in total.

Introduction to Path

The java.nio.file.Path interface represents a file or directory location in the file system. It replaces the legacy java.io.File class with a richer, platform-independent API.

import java.nio.file.*;

Path p1 = Path.of("/home/user/documents/report.txt");
Path p2 = Paths.get("/home", "user", "documents", "report.txt");

System.out.println(p1); // /home/user/documents/report.txt
System.out.println(p1.equals(p2)); // true

Path Components

Extract components of a path:

Path path = Path.of("/home/user/docs/report.txt");

System.out.println(path.getFileName()); // report.txt
System.out.println(path.getParent());   // /home/user/docs
System.out.println(path.getRoot());     // /
System.out.println(path.getNameCount()); // 4
System.out.println(path.getName(0));    // home
System.out.println(path.getName(3));    // report.txt

Relative vs Absolute Paths

A path can be absolute (starts from root) or relative (from current directory). Use isAbsolute() to check and toAbsolutePath() to resolve a relative path against the current working directory.

Path rel = Path.of("docs/report.txt");
Path abs = Path.of("/home/user/docs/report.txt");

System.out.println(rel.isAbsolute()); // false
System.out.println(abs.isAbsolute()); // true

Path resolved = rel.toAbsolutePath(); // resolves against CWD
System.out.println(resolved);

resolve: Combining Paths

resolve(other) appends another path. If other is absolute, it replaces the current path entirely.

Path base = Path.of("/home/user");
Path docs = base.resolve("documents");
Path file = docs.resolve("report.txt");

System.out.println(file); // /home/user/documents/report.txt

// Resolving an absolute path replaces:
Path abs = base.resolve(Path.of("/etc/config"));
System.out.println(abs); // /etc/config

relativize: Computing Relative Paths

relativize(other) computes the relative path from this path to other:

Path from = Path.of("/home/user");
Path to   = Path.of("/home/user/docs/report.txt");

Path rel = from.relativize(to);
System.out.println(rel); // docs/report.txt

// Verify:
System.out.println(from.resolve(rel)); // /home/user/docs/report.txt

normalize: Removing Redundancies

normalize() removes . and .. segments from a path:

Path messy = Path.of("/home/user/../user/./docs/./report.txt");
Path clean = messy.normalize();
System.out.println(clean); // /home/user/docs/report.txt

startsWith and endsWith

Check prefix/suffix relationships between paths:

Path p = Path.of("/home/user/docs/report.txt");

System.out.println(p.startsWith("/home/user")); // true
System.out.println(p.startsWith("/home/other")); // false
System.out.println(p.endsWith("report.txt"));    // true
System.out.println(p.endsWith("docs/report.txt")); // true

Converting to File and URI

Convert a Path to the legacy File class or a URI when needed:

Path path = Path.of("/home/user/report.txt");

java.io.File file = path.toFile();
System.out.println(file.exists()); // depends on filesystem

java.net.URI uri = path.toUri();
System.out.println(uri); // file:///home/user/report.txt

Path on Different Platforms

Path.of() uses the platform's separator. On Windows, paths use backslash; on Unix, forward slash. The API handles this transparently.

// Portable path construction (works on all platforms):
Path config = Path.of("config", "app", "settings.json");
// On Unix: config/app/settings.json
// On Windows: config\app\settings.json
System.out.println(config);

Iterating Path Elements

Use a for-each loop or iterator() to iterate over individual name elements:

Path path = Path.of("/home/user/docs/report.txt");

for (Path element : path) {
    System.out.println(element); // home, user, docs, report.txt
}
// Note: root ("/") is not included in iteration

subpath: Extracting a Slice

subpath(begin, end) returns a relative path for a range of name elements:

Path p = Path.of("/home/user/docs/report.txt");
// Indices: home=0, user=1, docs=2, report.txt=3

System.out.println(p.subpath(1, 3)); // user/docs
System.out.println(p.subpath(0, 2)); // home/user

Quick Check

Given Path.of("/home/user").resolve("docs/file.txt"), what is the result?

Recap: Path API

Key takeaways:

  • Path.of() / Paths.get() create Path instances
  • getFileName, getParent, getRoot, getName, getNameCount for components
  • resolve() appends; relativize() computes relative path; normalize() cleans ./ ../
  • startsWith/endsWith for prefix/suffix checks
  • Platform-independent — separator handled automatically
Free to start

Learn Java with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
104
Lessons
374

Frequently Asked Questions

Is the “Path: Representing File Locations” lesson free?

Yes — the full text of “Path: Representing File Locations” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO. The Java Academy course includes 4 lessons in total.

What will I learn in “Path: Representing File Locations”?

Create Path objects, resolve and relativize paths, and manipulate path components. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Path: Representing File Locations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Path: Representing File Locations
  2. Files Utility: Read, Write, Copy, Move
  3. Walking Directory Trees with Files.walk
  4. WatchService: Monitoring File Changes
← Back to Java Academy