Mastering Java File Paths: Relative, Absolute & Canonical Methods
This article explains Java file path concepts—including relative, absolute, and shortcut notations—and demonstrates how to read files using java.io.File with getPath, getAbsolutePath, and getCanonicalPath methods, complete with code examples.
1. Introduction
In Java development we often need to obtain file paths, for example to read configuration files. This article discusses file paths and how to read files.
2. File Paths
File paths are usually either relative paths or absolute paths.
2.1 Relative Path
A relative path is resolved based on the location of the current file. In Java code the reference point is the location of the running code; as long as the referenced file’s position relative to the source file does not change, it can be read.
2.2 Absolute Path
An absolute path is the real location of a file in the file system, starting from the root directory (a drive letter on Windows). It is like giving a house number; the exact location can be found without additional context.
2.3 Path Shortcuts
Common shortcut symbols (using Unix‑like systems as example):
Symbol
Description
../
Parent directory of the current directory
./
Current directory
/
Root directory
~
User home directory (e.g., /Users/username on mac, c:\users\username on Windows)
On Windows the separator / is usually replaced by \ .
3. Reading Files in Java
Assume the following project structure:
foo
|_src
|_Test.java
|_app.yml Test.javacontains the logic to read app.yml. Java provides java.io.File and three common methods to obtain a file’s path.
3.1 getPath()
Returns the abstract pathname as a string, i.e., the path passed to the File constructor. If the File was created with a relative path, getPath() returns a relative path; otherwise it returns an absolute path.
File file = new File("./app.yml");
System.out.println("path = " + file.getPath()); // path = ./app.yml
File file = new File("/Users/dax/IdeaProjects/foo/src/app.yml");
System.out.println("path = " + file.getPath()); // path = /Users/dax/IdeaProjects/foo/src/app.yml3.2 getAbsolutePath()
Returns the absolute path of the file. Note that the path is resolved relative to the compiled location of the class, and shortcut symbols are not expanded.
File file = new File("./app.yml");
System.out.println("absolutePath = " + file.getAbsolutePath());
// absolutePath = /Users/dax/IdeaProjects/foo/./app.ymlBecause shortcuts are not expanded, the same file can have many different absolute path representations.
3.3 getCanonicalPath()
Resolves the path by eliminating shortcuts and symbolic links, returning a unique canonical path. This operation accesses the file system and may be slower, so use it only when you need the exact location.
File file = new File("./app.yml");
System.out.println("canonicalPath = " + file.getCanonicalPath());
// canonicalPath = /Users/dax/IdeaProjects/foo/app.ymlA normalized path (without shortcuts) is unique for a fixed file location.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
