简体   繁体   English

Java-获取文件的最终“目标”

[英]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? 现在,我可以看到目标为... \\ 2ndDir \\ file.txt,但是如何运行命令检查目标?

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对象并运行getCanonicalPath()方法即可获得所需的内容。

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

...checking for exceptions, yada yada. ...检查异常情况,yada yada。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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