Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java

1. Overview

The java.io.File class has three methods — getPath()getAbsolutePath() and getCanonicalPath() — to obtain the filesystem path.

In this article, we’ll have a quick look at the differences between them and discuss a use case where you may choose to use one over the others.

2. Method Definitions and Examples

Let’s start by going over the definitions of the three methods, along with examples based on having the following directory structure present in the user’s home directory:

|-- maixuanviet
    |-- maixuanviet.txt
    |-- foo
    |   |-- foo-one.txt
    |   \-- foo-two.txt
    \-- bar
        |-- bar-one.txt
        |-- bar-two.txt
        \-- baz
            |-- baz-one.txt
            \-- baz-two.txt

2.1. getPath()

Simply put, getPath() returns the String representation of the file’s abstract pathname. This is essentially the pathname passed to the File constructor.

So, if the File object was created using a relative path, the returned value from getPath() method would also be a relative path.

If we invoke the following code from the {user.home}/maixuanvietdirectory:

File file = new File("foo/foo-one.txt");
String path = file.getPath();

The path variable would have the value:

foo/foo-one.txt  // on Unix systems
foo\foo-one.txt  // on Windows systems

Notice that for the Windows system, the name-separator character has changed from the forward slash(/) character, which was passed to the constructor, to the backslash (\) character. This is because the returned String always uses the platform’s default name-separator character.

2.2. getAbsolutePath()

The getAbsolutePath() method returns the pathname of the file after resolving the path for the current user directory — this is called an absolute pathname. So, for our previous example, file.getAbsolutePath() would return:

/home/username/maixuanviet/foo/foo-one.txt     // on Unix systems
C:\Users\username\maixuanviet\foo\foo-one.txt  // on Windows systems

This method only resolves the current directory for a relative path. Shorthand representations (such as “.” and “..”) are not resolved further. Hence when we execute the following code from the directory {user.home}/maixuanviet:

File file = new File("bar/baz/../bar-one.txt");
String path = file.getAbsolutePath();

The value of the variable path would be:

/home/username/maixuanviet/bar/baz/../bar-one.txt      // on Unix systems
C:\Users\username\maixuanviet\bar\baz\..\bar-one.txt   // on Windows systems

2.3. getCanonicalPath()

The getCanonicalPath() method goes a step further and resolves the absolute pathname as well as the shorthands or redundant names like “.” and “.. as per the directory structure. It also resolves symbolic links on Unix systems and converts the drive letter to a standard case on Windows systems.

So for the previous example, getCanonicalPath() method would return:

/home/username/maixuanviet/bar/bar-one.txt     // on Unix systems
C:\Users\username\maixuanviet\bar\bar-one.txt  // on Windows systems

Let’s take another example. Given current directory as ${user.home}/maixuanviet and File object created using the parameter new File(“bar/baz/./baz-one.txt”), the output for getCanonicalPath() would be:

/home/username/maixuanviet/bar/baz/baz-one.txt     // on Unix systems
C:\Users\username\maixuanviet\bar\baz\baz-one.txt  // on Windows Systems

It’s worth mentioning that a single file on the filesystem can have an infinite number of absolute paths since there’s an infinite number of ways shorthand representations can be used. However, the canonical path will always be unique since all such representations are resolved.

Unlike the last two methods, getCanonicalPath() may throw IOException because it requires filesystem queries.

For example, on Windows systems, if we create a File object with one of the illegal characters, resolving the canonical path will throw an IOException:

new File("*").getCanonicalPath();

3. Use Case

Let’s say we’re writing a method that takes in a File object as a parameter and saves its fully qualified name into a database. We don’t know whether the path is relative or contains shorthands. In this case, we may want to use getCanonicalPath().

However, since getCanonicalPath() reads the filesystem, it comes at a performance cost. If we are sure that there are no redundant names or symbolic links and drive letter case is standardized (if using a Windows OS), then we should prefer using getAbsoultePath().

4. Conclusion

In this quick tutorial, we covered the differences between the three File methods to get filesystem path. We have also shown a use case where one method may be preferred over the other.

Junit test class demonstrating the examples of this article can be found over on GitHub.

Related posts:

Tiêu chuẩn coding trong Java (Coding Standards)
What is Thread-Safety and How to Achieve it?
How to Read a Large File Efficiently with Java
Testing an OAuth Secured API with Spring MVC
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Hướng dẫn Java Design Pattern – Object Pool
Spring Boot - Admin Client
Comparing Long Values in Java
Using the Not Operator in If Conditions in Java
A Guide to Queries in Spring Data MongoDB
Guide to Java OutputStream
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Spring Security Basic Authentication
Java Program to Implement Max Heap
Java Program to Generate Random Numbers Using Probability Distribution Function
Java – Combine Multiple Collections
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Java Program to Solve any Linear Equation in One Variable
Checking for Empty or Blank Strings in Java
Java Program to Check Whether a Given Point is in a Given Polygon
Introduction to Using FreeMarker in Spring MVC
Getting Started with GraphQL and Spring Boot
Java Program to Check if a Matrix is Invertible
Debug a JavaMail Program
Spring MVC Content Negotiation
Set Interface trong Java
Java Program to Implement Efficient O(log n) Fibonacci generator
Java Program to implement Circular Buffer
Giới thiệu JDBC Connection Pool
Integer Constant Pool trong Java
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Program to Implement Miller Rabin Primality Test Algorithm