Getting Filesystem Paths in Java

Learn the difference between path, absolute and canonical paths. Also, learn to get the path of a file in Java using standard IO and New IO classes.

1. Relative, Absolute and Canonical Paths

1.1. Relative Path

A relative path is a path that describes the location of a file or folder in relative to the current working directory. Unlike absolute paths, relative paths contain information that is only relative to the current directory.

This is the path, which we generally provide in the File class’s constructor.

src\main\java\com\howtodoinjava\io\foo\foo.txt
src\main\java\com\howtodoinjava\io\foo\bar\bar.txt

1.2. Absolute Path

An absolute path always contains the root element and the complete directory hierarchy required to locate the file. There is no more information required further to access the file or path.

  • Absolute paths contain all the relevant information to find the resources indicated by an absolute URL.
  • Absolute paths may contain the shorthands like single and double dots in the file paths.
C:\Users\lokesh\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\foo.txt
C:\Users\lokesh\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\bar\..\foo.txt

1.3. Canonical Path

Canonical paths are absolute paths after resolving the shorthands or redundant names like dots “.” and “..” as per the directory structure.

The canonical path of a file just “purifies” the path to the absolute path, removing and resolving stuff like dots and resolving symlinks (on UNIX).

C:\Users\lokesh\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\foo.txt
C:\Users\lokesh\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\bar\bar.txt

2. Get File Paths Using java.io.File

The java.io.File class has three methods to obtain the filesystem paths:

  • file.getPath() : returns the same string passed as the File constructor.
  • file.getAbsolutePath() : returns the pathname of the file after resolving the path for the current user’s directory. It does not resolve the shorthands like single and double dots.
  • file.getCanonicalPath() : returns the full path after resolving the absolute pathname as well as the shorthands.
File file = new File("src/main/java/com/howtodoinjava/io/foo/bar/../foo.txt");

String givenPath = file.getPath();
String absPath = file.getAbsolutePath();
String canPath = file.getCanonicalPath();

System.out.println(givenPath);
System.out.println(absPath);
System.out.println(canPath);
src\main\java\com\howtodoinjava\io\foo\bar\..\foo.txt
C:\Users\lokes\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\bar\..\foo.txt
C:\Users\lokes\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\foo.txt

3. Get File Paths Using java.nio.file.Path

The java.nio.file.Path also provides following methods for getting various paths:

  • path.toAbsolutePath() – Full file path similar to file.getAbsolutePath().
  • path.toRealPath() – Canonical path similar to file.getCanonicalPath().

The Path methods throws NoSuchFileException if the file is not present in the specified location. The File methods do not throw any exceptions.

Path path = Path.of("src/main/java/com/howtodoinjava/io/foo/bar/../foo.txt");

givenPath = path.toString();
absPath = path.toAbsolutePath().toString();
canPath = path.toRealPath().toString();

System.out.println(givenPath);
System.out.println(absPath);
System.out.println(canPath);
src\main\java\com\howtodoinjava\io\foo\bar\..\foo.txt
C:\Users\lokes\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\bar\..\foo.txt
C:\Users\lokes\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\foo.txt

4. Conclusion

In this tutorial, we understood different kinds of filesystem paths in Java and how to get these paths using Java APIs.

As a best practice, always use the canonical paths if we are not sure if the provided path contains the shorthand characters or not.

Happy Learning !!

Source Code on Github

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial