Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting canonical path from Path in Java

Tags:

java

I have a Path object, but it appears it does not have a getCanonicalPath() method like in the File class. Reason is I have dots in the Path that I want to eliminate to get something "unique":

/some/path/./something 
# should be
/some/path/something
like image 408
Jiew Meng Avatar asked Mar 20 '23 16:03

Jiew Meng


2 Answers

See normalize and toRealPath.

like image 195
Puce Avatar answered Mar 23 '23 06:03

Puce


It seems that getRealPath() does the job. If not you can always call path.toFile().getCanonicalPath()

like image 27
AlexR Avatar answered Mar 23 '23 05:03

AlexR