In Java, for NIO Path, we can use path.toAbsolutePath() to get the file path; For legacy IO File, we can use file.getAbsolutePath() to get the file path.
For symbolic links or file path contains . or .., we can use path.toRealPath() or file.getCanonicalPath() to get the real file pah.
Below is the mapping of File and Path.
file.getAbsolutePath()<=>path.toAbsolutePath()file.getCanonicalPath()<=>path.toRealPath()file.getParent()<=>path.getParent()
1. Get file path of a file (NIO Path).
For java.nio.file.Path, we can use the following APIs to get the file path of a file.
path.toAbsolutePath()– Full file path.path.toRealPath())– For symbolic links or resolving the.or..symbols in the path name, default it follow link.path.getParent()– Get parent directory of the path.
1.1 Path = /home/tvt/workspace/favtuts/test/file.txt
Path path = Paths.get("/home/tvt/workspace/favtuts/test/file.txt");
path : /home/tvt/workspace/favtuts/test/file.txt
path.toAbsolutePath() : /home/tvt/workspace/favtuts/test/file.txt
path.getParent() : /home/tvt/workspace/favtuts/test
path.toRealPath() : /home/tvt/workspace/favtuts/test/file.txt
1.2 Path = file.txt, the file is refer to the current working directory + file.txt.
Path path = Paths.get("file.txt");
path : file.txt
path.toAbsolutePath() : /home/tvt/favtuts/java-core-tutorials-examples/java-io/file.txt
path.getParent() : null
path.toRealPath() : /home/tvt/favtuts/java-core-tutorials-examples/java-io/file.txt
1.3 Path = /home/tvt/workspace/favtuts/test/soft-link, a symbolic link.
$ ln -s /home/tvt/workspace/favtuts/test/test-b/test-d/test-d2.log /home/tvt/workspace/favtuts/test/soft-link
Path path = Paths.get("/home/tvt/workspace/favtuts/test/soft-link");
path : /home/tvt/workspace/favtuts/test/soft-link
path.toAbsolutePath() : /home/tvt/workspace/favtuts/test/soft-link
path.getParent() : /home/tvt/workspace/favtuts/test
path.toRealPath() : /home/tvt/workspace/favtuts/test/test-b/test-d/test-d2.log
1.4 Path = /home/tvt/workspace/favtuts/test/../hello.txt
Path path = Paths.get("/home/tvt/workspace/favtuts/test/../hello.txt");
path : /home/tvt/workspace/favtuts/test/../hello.txt
path.toAbsolutePath() : /home/tvt/workspace/favtuts/test/../hello.txt
path.getParent() : /home/tvt/workspace/favtuts/test..
path.toRealPath() : /home/tvt/workspace/favtuts/hello.txt
1.5 Below is a complete Java example to get the file path of different Path.
GetFilePath1.java
package com.favtuts.io.howto;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
public class GetFilePath {
public static void main(String[] args) {
// full path
Path path1 = Paths.get("/home/tvt/workspace/favtuts/test/file.txt");
System.out.println("\n[Path] : " + path1);
printPath(path1);
// file name
Path path2 = Paths.get("file.txt");
System.out.println("\n[Path] : " + path2);
printPath(path2);
// soft or symbolic link
Path path3 = Paths.get("/home/tvt/workspace/favtuts/test/soft-link");
System.out.println("\n[Path] : " + path3);
printPath(path3);
// a path contains `..`
Path path4 = Paths.get("/home/tvt/workspace/favtuts/test/../hello.txt");
System.out.println("\n[Path] : " + path4);
printPath(path4);
}
static void printPath(Path path) {
System.out.printf("%-25s : %s%n", "path", path);
System.out.printf("%-25s : %s%n", "path.toAbsolutePath()",
path.toAbsolutePath());
System.out.printf("%-25s : %s%n", "path.getParent()", path.getParent());
System.out.printf("%-25s : %s%n", "path.getRoot()", path.getRoot());
try {
if (Files.notExists(path)) {
return;
}
// default, follow symbolic link
System.out.printf("%-25s : %s%n", "path.toRealPath()",
path.toRealPath());
// no follow symbolic link
System.out.printf("%-25s : %s%n", "path.toRealPath(nofollow)",
path.toRealPath(LinkOption.NOFOLLOW_LINKS));
// alternative to check isSymbolicLink
/*if (Files.isSymbolicLink(path)) {
Path link = Files.readSymbolicLink(path);
System.out.println(link);
}*/
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
[Path] : /home/tvt/workspace/favtuts/test/file.txt
path : /home/tvt/workspace/favtuts/test/file.txt
path.toAbsolutePath() : /home/tvt/workspace/favtuts/test/file.txt
path.getParent() : /home/tvt/workspace/favtuts/test
path.getRoot() : /
path.toRealPath() : /home/tvt/workspace/favtuts/test/file.txt
path.toRealPath(nofollow) : /home/tvt/workspace/favtuts/test/file.txt
[Path] : file.txt
path : file.txt
path.toAbsolutePath() : /home/tvt/favtuts/java-core-tutorials-examples/java-io/file.txt
path.getParent() : null
path.getRoot() : null
path.toRealPath() : /home/tvt/favtuts/java-core-tutorials-examples/java-io/file.txt
path.toRealPath(nofollow) : /home/tvt/favtuts/java-core-tutorials-examples/java-io/file.txt
[Path] : /home/tvt/workspace/favtuts/test/soft-link
path : /home/tvt/workspace/favtuts/test/soft-link
path.toAbsolutePath() : /home/tvt/workspace/favtuts/test/soft-link
path.getParent() : /home/tvt/workspace/favtuts/test
path.getRoot() : /
path.toRealPath() : /home/tvt/workspace/favtuts/test/test-b/test-d/test-d2.log
path.toRealPath(nofollow) : /home/tvt/workspace/favtuts/test/soft-link
[Path] : /home/tvt/workspace/favtuts/test/../hello.txt
path : /home/tvt/workspace/favtuts/test/../hello.txt
path.toAbsolutePath() : /home/tvt/workspace/favtuts/test/../hello.txt
path.getParent() : /home/tvt/workspace/favtuts/test/..
path.getRoot() : /
path.toRealPath() : /home/tvt/workspace/favtuts/hello.txt
path.toRealPath(nofollow) : /home/tvt/workspace/favtuts/hello.txt
2. Get file path of a file (legacy File)
For the legacy IO java.io.File, we can use the following APIs to get the file path of a file.
file.getAbsolutePath()=path.toAbsolutePath()file.getCanonicalPath()=path.toRealPath()file.getParent()=path.getParent()
GetFilePath2.java
package com.favtuts.io.howto;
import java.io.File;
import java.io.IOException;
public class GetFilePath {
public static void main(String[] args) {
// Java Legacy IO
// full file path
File file1 = new File("/home/tvt/workspace/favtuts/test/file.txt");
System.out.println("[File] : " + file1);
printFilePath(file1);
// a file name
File file2 = new File("file.txt");
System.out.println("\n[File] : " + file2);
printFilePath(file2);
// a soft or symbolic link
File file3 = new File("/home/tvt/workspace/favtuts/test/soft-link");
System.out.println("\n[File] : " + file3);
printFilePath(file3);
// a file contain `..`
File file4 = new File("/home/tvt/workspace/favtuts/test/../hello.txt");
System.out.println("\n[File] : " + file4);
printFilePath(file4);
}
// If a single file name, not full path, the file refer to
// System.getProperty("user.dir") + file
static void printFilePath(File file) {
// print File = print file.getPath()
System.out.printf("%-25s : %s%n", "file.getPath()", file.getPath());
System.out.printf("%-25s : %s%n", "file.getAbsolutePath()",
file.getAbsolutePath());
try {
System.out.printf("%-25s : %s%n", "file.getCanonicalPath()",
file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
System.out.printf("%-25s : %s%n", "Parent Path", getParentPath(file));
}
// if unable to get parent, try substring to get the parent folder.
private static String getParentPath(File file) {
if (file.getParent() == null) {
String absolutePath = file.getAbsolutePath();
return absolutePath.substring(0,
absolutePath.lastIndexOf(File.separator));
} else {
return file.getParent();
}
}
}
Output
[File] : /home/tvt/workspace/favtuts/test/file.txt
file.getPath() : /home/tvt/workspace/favtuts/test/file.txt
file.getAbsolutePath() : /home/tvt/workspace/favtuts/test/file.txt
file.getCanonicalPath() : /home/tvt/workspace/favtuts/test/file.txt
Parent Path : /home/tvt/workspace/favtuts/test
[File] : file.txt
file.getPath() : file.txt
file.getAbsolutePath() : /home/tvt/favtuts/java-core-tutorials-examples/java-io/file.txt
file.getCanonicalPath() : /home/tvt/favtuts/java-core-tutorials-examples/java-io/file.txt
Parent Path : /home/tvt/favtuts/java-core-tutorials-examples/java-io
[File] : /home/tvt/workspace/favtuts/test/soft-link
file.getPath() : /home/tvt/workspace/favtuts/test/soft-link
file.getAbsolutePath() : /home/tvt/workspace/favtuts/test/soft-link
file.getCanonicalPath() : /home/tvt/workspace/favtuts/test/test-b/test-d/test-d2.log
Parent Path : /home/tvt/workspace/favtuts/test
[File] : /home/tvt/workspace/favtuts/test/../hello.txt
file.getPath() : /home/tvt/workspace/favtuts/test/../hello.txt
file.getAbsolutePath() : /home/tvt/workspace/favtuts/test/../hello.txt
file.getCanonicalPath() : /home/tvt/workspace/favtuts/hello.txt
Parent Path : /home/tvt/workspace/favtuts/test/..
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-io/howto