Reputation: 8702
I want to clean the path I use in my App. The path can be modified and sometimes I got something like that:
C:/users/Username/Desktop/\..\..\..\Windows\Web\..\..\Program Files\..\Program Files\..\Python27\
But I would like to have something like:
C:\Python27\
That's an example!
How can I clean the path to get only the necessary part?
Thanks.
Upvotes: 2
Views: 8331
Reputation: 48837
You could try using the File.getCanonicalPath() method:
File file = new File("my/init/path");
String path = file.getCanonicalPath();
I haven't test though, tell us back!
EDIT: @MathiasSchwarz is right, use getCanonicalPath() instead of getAbsolutePath() (link)
Upvotes: 3
Reputation: 115388
Here is the code I have just tried.
new File("c:/temp/..").getCanonicalPath();
It returns 'C:\', that is right. The parent of c:/temp
is indeed c:\
Upvotes: 3
Reputation: 7197
If fileName
is your filename string, then something like:
String cleanedFilename = new File(fileName).getCanonicalPath();
should do it...
Se also the API description.
Upvotes: 5