简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM