What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?
Consider these filenames:
C:\temp\file.txt
- This is a path, an absolute path, and a canonical path.
.\file.txt
- This is a path. It's neither an absolute path nor a canonical path.
C:\temp\myapp\bin\..\\..\file.txt
- This is a path and an absolute path. It's not a canonical path.
A canonical path is always an absolute path.
Converting from a path to a canonical path makes it absolute (usually tack on the current working directory so e.g. ./file.txt
becomes c:/temp/file.txt
). The canonical path of a file just "purifies" the path, removing and resolving stuff like ..\
and resolving symlinks (on unixes).
Also note the following example with nio.Paths:
String canonical_path_string = "C:\\Windows\\System32\\";
String absolute_path_string = "C:\\Windows\\System32\\drivers\\..\\";
System.out.println(Paths.get(canonical_path_string).getParent());
System.out.println(Paths.get(absolute_path_string).getParent());
While both paths refer to the same location, the output will be quite different:
C:\Windows
C:\Windows\System32\drivers
what's the difference between canonicalpath and absolutepath?
The difference is that there is only one canonical path to a file[1], while there can be many absolute paths to a file (depending on the system). For instance, on a Unix system, /usr/local/../bin
is the same as /usr/bin
. getCanonicalPath()
resolves those ambiguities and returns the (unique) canonical path. So if the current directory was /usr/local
, then:
File file = new File("../bin");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
would print:
../bin
/usr/local/../bin
/usr/bin
Per Voo's suggestion: on Unix systems, getCanonicalPath()
will also resolve symbolic links if the symbolic link exists. Hard links are treated like normal files (which is basically what they are). Note, however, that a file need not exist for these methods to succeed.
[1] Well, not quite. As @Tom Hale points out in a comment, if the file system supports hard-linked directories, there may be multiple canonical paths to a given file.
getCanonicalPath different from getAbsolutePath - adding /private
You should not run ls -al
on the final path. If you were looking for a symlink you would have to step from the first folder downwards to the file. Luckily this process is quickly over since ls -la /
yields amongst others:
lrwxr-xr-x@ 1 root wheel 11 Jan 11 14:16 var -> private/var
Meaning it is in fact a symlink and therefore the canonical paths "expands" the link.
What is the different between getAbsolutePath and getCanonicalPath
Suppose /home
was actually a symbolic link to /usr/home
. Then getAbsolutePath
would still return /home/kit.ho/
whereas getCanonicalPath
would resolve the symlink and return /usr/home/kit.ho/
.
Difference between getCanonicalPath and toRealPath
Conclusions:
getAbsolutePath
andgetPath
never fail as they don't do validationgetCanonicalPath
reach invalid results when drive letter from url is invalid or different than the current foldertoPath().toRealPath()
is checking the validity but the file needs to exist and can also follow or not follow symbolic linkstoPath()
is safe enough and doesn't need the file to exist..toPath().toAbsolutePath().normalize()
is the best one without the need for file to exist
I did a similar test of @John in windows
@Test
public void testCanonical() throws IOException {
test("d:tarGet\\..\\Target", "File exist and drive letter is on the current one");
test("d:tarGet\\..\\Target\\.\\..\\", "File exist and drive letter is on the current one, but parent of current drive should exist");
test("d:tarGet\\non-existent\\..\\..\\Target\\.\\..\\", "Relative path contains non-existent file");
test("d:target\\\\file", "Double slash");
test("c:tarGet\\..\\Target\\.", "File doesn't exist and drive letter is on different drive than the current one");
test("l:tarGet\\..\\Target\\.\\..\\", "Drive letter doesn't exist");
test("za:tarGet\\..\\Target\\.\\..\\", "Drive letter is double so not valid");
test("d:tarGet|Suffix", "Path contains invalid chars in windows (|)");
test("d:tarGet\u0000Suffix", "Path contains invalid chars in both linux and windows (\\0)");
}
private void test(String filename, String message) throws IOException {
java.io.File file = new java.io.File(filename);
System.out.println("Use: " + filename + " -> " + message);
System.out.println("F-GET: " + Try.of(() -> file.getPath()));
System.out.println("F-ABS: " + Try.of(() -> file.getAbsolutePath()));
System.out.println("F-CAN: " + Try.of(() -> file.getCanonicalPath()));
System.out.println("P-TO: " + Try.of(() -> file.toPath()));
System.out.println("P-ABS: " + Try.of(() -> file.toPath().toAbsolutePath()));
System.out.println("P-NOR: " + Try.of(() -> file.toPath().normalize()));
System.out.println("P-NOR-ABS: " + Try.of(() -> file.toPath().normalize().toAbsolutePath()));
System.out.println("P-ABS-NOR: " + Try.of(() -> file.toPath().toAbsolutePath().normalize()));
System.out.println("P-REAL: " + Try.of(() -> file.toPath().toRealPath()));
System.out.println("");
}
The results are:
Use: d:tarGet\..\Target -> File exist and drive letter is on the current one
F-GET: Success(d:tarGet\..\Target)
F-ABS: Success(d:\home\raiser\work\restfs\tarGet\..\Target)
F-CAN: Success(D:\home\raiser\work\restfs\target)
P-TO: Success(d:tarGet\..\Target)
P-ABS: Success(D:\home\raiser\work\restfs\tarGet\..\Target)
P-NOR: Success(d:Target)
P-NOR-ABS: Success(D:\home\raiser\work\restfs\Target)
P-ABS-NOR: Success(D:\home\raiser\work\restfs\Target)
P-REAL: Success(D:\home\raiser\work\restfs\target)
Use: d:tarGet\..\Target\.\..\ -> File exist and drive letter is on the current one, but parent of current drive should exist
F-GET: Success(d:tarGet\..\Target\.\..)
F-ABS: Success(d:\home\raiser\work\restfs\tarGet\..\Target\.\..)
F-CAN: Success(D:\home\raiser\work\restfs)
P-TO: Success(d:tarGet\..\Target\.\..)
P-ABS: Success(D:\home\raiser\work\restfs\tarGet\..\Target\.\..)
P-NOR: Success(d:)
P-NOR-ABS: Success(D:\home\raiser\work\restfs\)
P-ABS-NOR: Success(D:\home\raiser\work\restfs)
P-REAL: Success(D:\home\raiser\work\restfs)
Use: d:tarGet\non-existent\..\..\Target\.\..\ -> Relative path contains non-existent file
F-GET: Success(d:tarGet\non-existent\..\..\Target\.\..)
F-ABS: Success(d:\home\raiser\work\restfs\tarGet\non-existent\..\..\Target\.\..)
F-CAN: Success(D:\home\raiser\work\restfs)
P-TO: Success(d:tarGet\non-existent\..\..\Target\.\..)
P-ABS: Success(D:\home\raiser\work\restfs\tarGet\non-existent\..\..\Target\.\..)
P-NOR: Success(d:)
P-NOR-ABS: Success(D:\home\raiser\work\restfs\)
P-ABS-NOR: Success(D:\home\raiser\work\restfs)
P-REAL: Success(D:\home\raiser\work\restfs)
Use: d:target\\file -> Double slash
F-GET: Success(d:target\file)
F-ABS: Success(d:\home\raiser\work\restfs\target\file)
F-CAN: Success(D:\home\raiser\work\restfs\target\file)
P-TO: Success(d:target\file)
P-ABS: Success(D:\home\raiser\work\restfs\target\file)
P-NOR: Success(d:target\file)
P-NOR-ABS: Success(D:\home\raiser\work\restfs\target\file)
P-ABS-NOR: Success(D:\home\raiser\work\restfs\target\file)
P-REAL: Failure(java.nio.file.NoSuchFileException: D:\home\raiser\work\restfs\target\file)
Use: c:tarGet\..\Target\. -> File doesn't exist and drive letter is on different drive than the current one
F-GET: Success(c:tarGet\..\Target\.)
F-ABS: Success(c:\\tarGet\..\Target\.)
F-CAN: Success(C:\Target)
P-TO: Success(c:tarGet\..\Target\.)
P-ABS: Success(C:\tarGet\..\Target\.)
P-NOR: Success(c:Target)
P-NOR-ABS: Success(C:\Target)
P-ABS-NOR: Success(C:\Target)
P-REAL: Failure(java.nio.file.NoSuchFileException: C:\Target)
Use: l:tarGet\..\Target\.\..\ -> Drive letter doesn't exist
F-GET: Success(l:tarGet\..\Target\.\..)
F-ABS: Success(l:\tarGet\..\Target\.\..)
F-CAN: Success(L:\)
P-TO: Success(l:tarGet\..\Target\.\..)
P-ABS: Failure(java.io.IOError: java.io.IOException: Unable to get working directory of drive 'L')
P-NOR: Success(l:)
P-NOR-ABS: Failure(java.io.IOError: java.io.IOException: Unable to get working directory of drive 'L')
P-ABS-NOR: Failure(java.io.IOError: java.io.IOException: Unable to get working directory of drive 'L')
P-REAL: Failure(java.io.IOException: Unable to get working directory of drive 'L')
Use: za:tarGet\..\Target\.\..\ -> Drive letter is double so not valid
F-GET: Success(za:tarGet\..\Target\.\..)
F-ABS: Success(D:\home\raiser\work\restfs\za:tarGet\..\Target\.\..)
F-CAN: Success(D:\home\raiser\work\restfs)
P-TO: Failure(java.nio.file.InvalidPathException: Illegal char <:> at index 2: za:tarGet\..\Target\.\..)
P-ABS: Failure(java.nio.file.InvalidPathException: Illegal char <:> at index 2: za:tarGet\..\Target\.\..)
P-NOR: Failure(java.nio.file.InvalidPathException: Illegal char <:> at index 2: za:tarGet\..\Target\.\..)
P-NOR-ABS: Failure(java.nio.file.InvalidPathException: Illegal char <:> at index 2: za:tarGet\..\Target\.\..)
P-ABS-NOR: Failure(java.nio.file.InvalidPathException: Illegal char <:> at index 2: za:tarGet\..\Target\.\..)
P-REAL: Failure(java.nio.file.InvalidPathException: Illegal char <:> at index 2: za:tarGet\..\Target\.\..)
Use: d:tarGet|Suffix -> Path contains invalid chars in windows (|)
F-GET: Success(d:tarGet|Suffix)
F-ABS: Success(d:\home\raiser\work\restfs\tarGet|Suffix)
F-CAN: Failure(java.io.IOException: The filename, directory name, or volume label syntax is incorrect)
P-TO: Failure(java.nio.file.InvalidPathException: Illegal char <|> at index 8: d:tarGet|Suffix)
P-ABS: Failure(java.nio.file.InvalidPathException: Illegal char <|> at index 8: d:tarGet|Suffix)
P-NOR: Failure(java.nio.file.InvalidPathException: Illegal char <|> at index 8: d:tarGet|Suffix)
P-NOR-ABS: Failure(java.nio.file.InvalidPathException: Illegal char <|> at index 8: d:tarGet|Suffix)
P-ABS-NOR: Failure(java.nio.file.InvalidPathException: Illegal char <|> at index 8: d:tarGet|Suffix)
P-REAL: Failure(java.nio.file.InvalidPathException: Illegal char <|> at index 8: d:tarGet|Suffix)
Is there a way to distinguish between 2XX and 304 responses in Gatling and show it in the report?
Not with the free version.
This chart is available in the Enterprise version though.
Related Topics
Prime Numbers by Eratosthenes Quicker Sequential Than Concurrently
Listener Placement Adhering to the Traditional (Non-Mediator) MVC Pattern
How to Have Explained the Difference Between an Interface and an Abstract Class
Most Efficient Way to Increment a Map Value in Java
Using Java with Nvidia Gpus (Cuda)
Why Does Math.Round(0.49999999999999994) Return 1
Difference Between Jar and War in Java
Warning About Ssl Connection When Connecting to MySQL Database
Static Nested Class in Java, Why
Reversing a Linked List in Java, Recursively
Import Pem into Java Key Store
How to Implement a Re-Try-Catch
When Should I Use File.Separator and When File.Pathseparator