A Path represents a path that is hierarchical and composed of a sequence of directory and file name elements separated by a special separator or delimiter.java.io.File contains methods for determining the file path.

 A Path is considered to be an empty path if it consists solely of one name element that is empty.

Table of Content :

Accessing a file using an empty path is equivalent to accessing the default directory of the file system. Path defines the getFileName, getParent, getRoot, and subpath methods to access the path components or a subsequence of its name elements.

Absolute Path
The path with reference to the root directory is called absolute.

Relative Path
The path with reference to the current directory is called relative.

Creating a Path Instance

In order to use a java.nio.file.Path, we need to create a Path instance. You create a Path instance using a static method in the Paths class (java.nio.file.Paths) named Paths.get().

import java.nio.file.Path; import java.nio.file.Paths; public class PathExample { public static void main(String[] args) { Path path = Paths.get("H:\\files\\myfile.txt"); } }

Creating an Absolute Path

It can be created by calling the Paths.get() factory method with the absolute file as a parameter.

Path path = Paths.get("H:\\files\\abc.txt"); //For Windows Path path = Paths.get("/home/devuser/files/abc.txt"); // For Linux

Creating a Relative Path 

The Java NIO Path class can also be used to work with relative paths. You create a relative path using the Paths.get(basePath, relative path) method. Here are two relative path examples in Java.

Path logs = Paths.get("H:\\files", "logs"); Path projectLogs = Paths.get("H:\\data", "projects\\logs\\log1.txt");

JavaFilePathExample.java.

import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; public class JavaFilePathExample { public static void main(String[] args) throws IOException, URISyntaxException { File file = new File("/home/devuser/files/abc.txt"); displayPaths(file); // relative path file = new File("pqr.txt"); displayPaths(file); // complex relative paths file = new File("/home/devuser/../abc.txt"); displayPaths(file); // URI paths file = new File(new URI("file://home/devuser/files/abc.txt")); displayPaths(file); } private static void displayPaths(File file) throws IOException { System.out.println("Absolute Path: " + file.getAbsolutePath()); System.out.println("Canonical Path: " + file.getCanonicalPath()); System.out.println("Path: " + file.getPath()); } }

Post/Questions related to Java File Path, Absolute Path, and Relative Path

How do I change the path of a file in Java?
How to get a file from a path in java?
How to split the file path in java? ?
How to read a file from a specified path in java?
How to convert string to the path in java?
How to pass the file path as a parameter in java?
How to get the file name from the path in java?

In this article, we have seen Java File Path, Absolute Path, and Relative Path with Examples. All source code in the article can be found in the GitHub repository.