01 / 22

File Paths &
Working Directory

Master how Java programs locate and access files using absolute paths, relative paths, and the working directory concept.

Java

What Is a File Path?

A file path is a string that tells the operating system (and your Java program) where a file lives on disk. Think of it as the address of a file: just as a street address leads you to a house, a file path leads your program to the right file.

The File System as a Tree

/ (root) Users etc var alex Documents projects MyApp.java Full path: /Users/alex/projects/ MyApp.java

Why Paths Matter in Java

Reading Files
Scanner, BufferedReader, and Files.readAllLines all require a valid path to locate data.
Writing Files
PrintWriter and Files.write create output at the path you specify: wrong path means wrong location.
FileNotFound
The most common beginner error. If Java cannot resolve your path, it throws a FileNotFoundException.

Absolute vs Relative Paths

Absolute Path
Starts from the root of the file system. Always points to the same file, regardless of where the program runs.

/Users/alex/projects/data.txt
C:\Users\alex\projects\data.txt
Relative Path
Starts from the current working directory. The actual file it points to depends on where the program was launched.

data.txt
../config/settings.txt

The Working Directory

The working directory (also called the current directory) is the folder from which your Java program was launched. It is the starting point for resolving every relative path.

// Print the working directory System.out.println(System.getProperty("user.dir")); // Output might be: /Users/alex/projects

How Java Resolves Paths

Working Directory /Users/alex/projects + Relative Path data/input.txt = Resolved Absolute Path /Users/alex/projects/data/input.txt /Users/alex/projects working dir + ../config/app.cfg .. means "go up" = /Users/alex/ config/app.cfg

Absolute vs Relative: Side by Side

Absolute Path Relative Path
Starts from File system root (/ or C:\) Working directory
Portability Machine-specific Portable across machines
Breaks when User name or install path changes Working directory changes
Best for System configs, temp files Project data files
Java check path.isAbsolute() == true path.isAbsolute() == false

Step-by-Step Path Strategy

1
Identify your working directory. Print System.getProperty("user.dir") to see where Java thinks "here" is.
2
Choose relative over absolute. Use relative paths for any file that ships with your project (data files, configs, test inputs).
3
Use File.separator or Paths.get(). Let Java handle OS differences. Avoid hardcoding / or \.
4
Verify the file exists. Call file.exists() or Files.exists(path) before reading. Provide a clear error message if missing.
5
Normalize when combining. Use path.resolve() and path.normalize() to clean up .. and . segments.

Example 1: Simple Relative Path

MyProject/ Main.java scores.txt Working dir: MyProject/ Path used: "scores.txt" // Main.java File f = new File( "scores.txt" ); ✓ Works!

Example 2: File in a Subdirectory

MyProject/ Main.java data/ input.csv // Main.java Scanner sc = new Scanner( new File( "data/input.csv" )); ✓ Correct relative path

Example 3: Going Up with ..

workspace/ MyProject/ shared/ Main.java utils.dat "../shared/utils.dat" .. goes up to workspace/, then into shared/

Modern Java: Path & Paths

Java's java.nio.file package offers a cleaner API than the old java.io.File class. The Path interface and Paths utility provide better path manipulation and OS independence.

import java.nio.file.Path; import java.nio.file.Paths; Path base = Paths.get("data", "input.csv"); // Creates: data/input.csv (or data\input.csv on Windows) Path full = base.toAbsolutePath(); // /Users/alex/projects/data/input.csv Path clean = Paths.get("data/../data/input.csv").normalize(); // data/input.csv (.. is resolved away)

IDE vs Command Line

IDE (IntelliJ, Eclipse)
Working directory defaults to the project root folder. Files at the top level of your project are found with just their name.
Command Line
Working directory is whatever folder your terminal is currently in when you type java MyApp. This often differs from the IDE default.

Cross-Platform Path Safety

1
Use forward slashes in string literals. Java's File and Path classes accept "/" on all platforms, including Windows.
2
Or use File.separator. This constant is "/" on Unix and "\\" on Windows, for maximum safety.
3
Never hardcode backslashes. Writing "data\\file.txt" breaks on macOS/Linux and requires escape characters.
4
Best practice: use Paths.get(). Pass each path segment as a separate argument: Paths.get("data", "file.txt").

Common Mistakes

✕ Hardcoded Absolute Path
Using "/Users/alex/data.txt" works only on your machine. It fails for every other developer or server.
✕ Wrong Working Directory
Assuming the working directory is the src/ folder when the IDE sets it to the project root, causing paths to miss one level.
✕ Backslash in Strings
Writing "data\file.txt" without escaping: Java interprets \f as a form-feed character, not a path separator.

Summary of Key Concepts

Concept Description Java Example
Absolute Path Full path from root /home/user/data.txt
Relative Path Path from working dir data/input.csv
Working Dir Where program launched System.getProperty("user.dir")
Parent Ref (..) Go up one directory ../config/app.cfg
Current Ref (.) Current directory (explicit) ./data.txt
Paths.get() OS-safe path creation Paths.get("data","f.txt")

Question 1 of 5

Which of the following is an absolute path?

Question 2 of 5

A Java program is run from /home/user/project. What absolute path does "data/report.txt" resolve to?

Question 3 of 5

What does the .. symbol mean in a file path?

Question 4 of 5

Which Java method returns the current working directory as a String?

Question 5 of 5

Your code works in IntelliJ but throws FileNotFoundException when run from the terminal. What is the most likely cause?