Get absolute file path example shows how to get the absolute path of a file in Java. The example also shows the difference between path, absolute path, and canonical path.
How to get the absolute file path in Java?
We can use the getAbsolutePath
method of Java File class to get the absolute file path.
1 |
public String getAbsolutePath() |
This method returns the absolute path of the file as a string.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.javacodeexamples.ioexamples; import java.io.File; public class FileAbsolutePathExample { public static void main(String[] args) { //String containing file path String strFilePath = "data.txt"; //file object File file = new File(strFilePath); /* * Use getAbsolutePath method of the File class * to get file's absolute path in file system. */ System.out.println( file.getAbsolutePath() ); } } |
Output
1 |
C:\JavaCodeExamples\workspace\JavaCodeExamples\data.txt |
As you can see from the output, I created the file without specifying the complete path but I when printed the absolute path, it gave the complete path of the file in the file system.
What is the difference between path, absolute path, and canonical path?
Java File class provides three methods namely getPath
, getAbsolutePath
and getCanonicalPath
. All three methods return a string containing the path information of the file, but they differ from one another.
getPath(): This method returns a path that was used to create a File object. In short, it is the path using which the File object was first created.
getAbsolutePath(): This method returns a path that is a fully qualified path (after resolving the path relative to the current directory, if the relative path was used while creating the File object).
getCanonicaPath(): This method returns the path which is similar to the absolute path but it also converts .
(Current directory) and ..
(Parent directory) to the actual directories. It also resolves any symbolic links before returning the canonical path.
There can be only one canonical path but there could be many absolute paths for the same file. Sounds confusing? Let’s see an example.
1 2 3 |
C:\dir1\..\one.txt C:\dir1\dir2\..\..\one.txt C:\one.txt |
Above given all paths are absolute paths for the same file one.txt. However, in this case, the canonical path would be c:\one.txt.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.javacodeexamples.ioexamples; import java.io.File; import java.io.IOException; public class FileAbsolutePathExample { public static void main(String[] args) throws IOException { printPathInfo("C:/data.txt"); printPathInfo("../data.txt"); printPathInfo("/data.txt"); printPathInfo("data.txt"); printPathInfo("./data.txt"); printPathInfo("C:/dir_1/../data.txt"); printPathInfo("C://dir_1//dir_2/../../data.txt"); } private static void printPathInfo(String strFilePath) throws IOException{ File file = new File(strFilePath); System.out.println("String path: " + strFilePath); System.out.println("getPath: " + file.getPath()); System.out.println("getAbsolutePath: " + file.getAbsolutePath()); System.out.println("getCanonicalPath: " + file.getCanonicalPath()); System.out.println(""); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
String path: C:/data.txt getPath: C:\data.txt getAbsolutePath: C:\data.txt getCanonicalPath: C:\data.txt String path: ../data.txt getPath: ..\data.txt getAbsolutePath: C:\JavaCodeExamples\workspace\JavaCodeExamples\..\data.txt getCanonicalPath: C:\JavaCodeExamples\workspace\data.txt String path: /data.txt getPath: \data.txt getAbsolutePath: C:\data.txt getCanonicalPath: C:\data.txt String path: data.txt getPath: data.txt getAbsolutePath: C:\JavaCodeExamples\workspace\JavaCodeExamples\data.txt getCanonicalPath: C:\JavaCodeExamples\workspace\JavaCodeExamples\data.txt String path: ./data.txt getPath: .\data.txt getAbsolutePath: C:\JavaCodeExamples\workspace\JavaCodeExamples\.\data.txt getCanonicalPath: C:\JavaCodeExamples\workspace\JavaCodeExamples \data.txt String path: C:/dir_1/../data.txt getPath: C:\dir_1\..\data.txt getAbsolutePath: C:\dir_1\..\data.txt getCanonicalPath: C:\data.txt String path: C://dir_1//dir_2/../../data.txt getPath: C:\dir_1\dir_2\..\..\data.txt getAbsolutePath: C:\dir_1\dir_2\..\..\data.txt getCanonicalPath: C:\data.txt |
Note: My current working directory is C:\JavaCodeExamples\workspace\JavaCodeExamples. Your output will be different according to your current working directory.
Also, the getPath
, getAbsolutePath
and getCanonicalPath
methods return systems file path separator. I am using windows, so it returned path separated by \
even though I created file object using /
or //
file separators.
This example is a part of the Java File tutorial with examples.
Please let me know your views in the comments section below.