XMan3 XMan3 - 2 months ago 6
Java Question

Java - Get final "destination" of a file

I was wondering, how can I find the final destination of a file?

For example, if I write this path:

C:\JavaDir\2ndDir\file.txt


The path would be simple, but if I type, for example:

C:\JavaDir\2ndDir\3rdDir\..\file.txt


Now, I can see that the destination would be ...\2ndDir\file.txt, but how can I run a command to check the destination?

Answer

What you want is the canonical path, which is unique for every single file. You basically just need to create a File object and run the getCanonicalPath() method to get what you want.

File f = new File(yourPath);
System.out.println(f.getCanonicalPath());

...checking for exceptions, yada yada.