Friday, September 6, 2019

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

While using Java IO if you ever tried to get path of the file using methods of the File class you would have seen three methods getPath(), getAbsolutePath() and getCanonicalPath() for getting the path of the file. If you ever wondered why these three methods and what are the differences among these three methods getPath(), getAbsolutePath() and getCanonicalPath() in Java then this article tries to explain these methods and the differences.

File path – Absolute and Relative

User interfaces and operating systems use system-dependent pathname strings to name files and directories. File class in Java presents an abstract, system-independent view of hierarchical pathnames.

When an abstract pathname is converted into a pathname string it is inherently system-dependent, each name is separated from the next by a single separator character which is "/" for Unix and "\" for Windows.

File pathname, whether abstract or in string form, may be either absolute or relative.

An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. For example – C:\Users\netjs\Test.txt

Using absolute pathname in a program or script is like hardcoding the file name which requires file to be always present in the given pathname.

A relative pathname is resolved against some other pathname (current user directory). Using relative pathname in a program or script is consider better option as path is resolved as per the location from where the program or script is executed.

getPath() method in File class

This method converts abstract pathname into a pathname string. If the File object is created using a relative path then the path returned is a relative path, if it’s created using an absolute path then the path returned is an absolute path.

The resulting string uses the separator character to separate the names in the name sequence. On UNIX systems the value of this separator field is '/'; on Microsoft Windows systems it is '\\'.

// absolute path
File file = new File("F://Temp/abc.txt");
System.out.println(file.getPath());

Output

F:\Temp\abc.txt

You can see absolute path is returned as the object is created using an absolute path. Also separator is changed for Windows system as the separator character is system dependent.

// relative path
File file = new File("Temp\\abc.txt");
System.out.println(file.getPath());

Output

Temp\abc.txt

You can see relative path is returned as the object is created using relative path.

Same example in Linux system-

// relative path
File file = new File("Temp/abc.txt");
System.out.println("Path- " + file.getPath());
System.out.println("Absolute Path- " + file.getAbsolutePath());

Output

Path- Temp/abc.txt
Absolute Path- /home/netjs/java-workspace/Temp/abc.txt

getAbsolutePath() method in Java File class

This method returns the absolute pathname string of the abstract file pathname. If this abstract pathname is already absolute, then the same pathname string is returned. Otherwise this pathname is resolved in a system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory. On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.

public class FilePath {
 public static void main(String[] args) {
  // relative path
  File file = new File("Temp\\abc.txt");
  System.out.println(file.getAbsolutePath());
 }
}

Output

F:\Anshu\NetJs\NetJS\Temp\abc.txt

In the example relative pathname is passed while creating file object which is resolved to the current user directory.

Note that there may be more than one absolute path pointing to the same file. For examples following absolute paths are for the same path (F:\Temp\abc.txt)-

F:\Temp\abc.txt

F:\\Temp\\..\\Temp\\abc.txt

F:\\Temp\\.\\abc.txt

Here ../ refers parent directory where as ./ refers current directory.

In case of getAbsolutePath() method redundant names such as "." and ".." are not resolved.

getCanonicalPath() method in Java File class

This method returns the canonical pathname string of this abstract pathname. A canonical pathname is both absolute and unique. This method converts the pathname to absolute form same as done by getAbsolutePath() method. How getCanonicalPath() differs from getAbsolutePath() is that this method removes redundant names such as "." and ".." from the pathname, resolves symbolic links (on UNIX platforms) and converts drive letters to a standard case (on Microsoft Windows platforms).

File file = new File("F:\\Temp\\abc.txt");
System.out.println("Absolute path- " + file.getAbsolutePath());
try {
 System.out.println("Canonical path- " + file.getCanonicalPath());
} catch (IOException e) {
 e.printStackTrace();
}

Output

Absolute path- F:\Temp\abc.txt
Canonical path- F:\Temp\abc.txt

In this case both absolute path and canonical paths are same.

File file = new File("f:\\Temp\\..\\Temp\\abc.txt");
System.out.println("Absolute path- " + file.getAbsolutePath());
try {
 System.out.println("Canonical path- " + file.getCanonicalPath());
} catch (IOException e) {
 e.printStackTrace();
}

Output

Absolute path- f:\Temp\..\Temp\abc.txt
Canonical path- F:\Temp\abc.txt

In this code you can see the difference between the getAbsolutePath() and getCanonicalPath() methods. As shown in the output getCanonicalPath() method removed "." and ".." from the pathname also drive letter is converted to a standard case (in Windows).

File file = new File("F:\\Temp\\.\\abc.txt");
System.out.println("Absolute path- " + file.getAbsolutePath());
try {
 System.out.println("Canonical path- " + file.getCanonicalPath());
} catch (IOException e) {
 e.printStackTrace();
}

Output

Absolute path- F:\Temp\.\abc.txt
Canonical path- F:\Temp\abc.txt

Again you can see that though absolute path can be written in many different ways for the same path but canonical path is unique.

File path example in Linux system-

File file = new File("/home/netjs/java-workspace/./Temp/abc.txt");
System.out.println("Absolute Path- " + file.getAbsolutePath());
try {
 System.out.println("Canonical Path- " + file.getCanonicalPath());
} catch (IOException e) {

 e.printStackTrace();
}

Output

Absolute Path- /home/netjs/java-workspace/./Temp/abc.txt
Canonical Path- /home/netjs/java-workspace/Temp/abc.txt

That's all for this topic getPath(), getCanonicalPath() and getAbsolutePath() Methods in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. Write to a File in Java
  2. Read or List All Files in a Folder - Java Program
  3. How to Find Last Modified Date of a File in Java
  4. Reading File in Java Using Files.lines And Files.newBufferedReader
  5. How to Write Excel File in Java Using Apache POI

You may also like-

  1. Java Variable Types With Examples
  2. java.lang.ClassCastException - Resolving ClassCastException in Java
  3. How to Loop Through HashSet in Java
  4. Thread Priorities in Java Multi-Threading
  5. Serialization Proxy Pattern in Java
  6. Java Concurrency Interview Questions And Answers
  7. Spring JdbcTemplate With ResultSetExtractor Example
  8. Spring Integration With Quartz Scheduler

No comments:

Post a Comment