获取不带驱动器字母的文件路径 – Windows 10 / Java 8

huangapple go评论75阅读模式
英文:

Get path of file without drive letter - Windows 10 / Java 8

问题

当我在File对象上调用getCanonicalPath时,我会得到一个像这样的字符串:

C:\data\processed\Test.xml

如何获得相同的字符串,但去除掉C:\,如果可能的话,还要用/代替\

英文:

Windows 10
Java 8

When I call getCanonicalPath on a File object, I get a string like this

C:\data\processed\Test.xml

How do I get the same string but without C:\ and if possible also with / instead of \?

答案1

得分: 4

你可以使用 NIO.2 API 以及其对象 PathPaths,它是对文件系统的一种抽象。

Path path = Paths.get("C:\\data\\processed\\Test.xml");

你也可以通过 File::toPathFile 得到 Path。实际上,你需要获取路径中的所有 名称

File file = new File("C:\\data\\processed\\Test.xml");
Path path = file.toPath();
int count = path.getNameCount();                        // 名称的数量
path = path.subpath(0, count);                          // 所有的名称

或者你也可以使用 Path:relativize(感谢 @Holger 的建议),它可以找到相对于根目录 C:/ 的相对路径,其中包含了所有的 名称

File file = new File("C:\\data\\processed\\Test.xml");
Path path = file.toPath();
path = path.getRoot().relativize(path);

以下是一些相关的方法:

  • path.getRoot() 返回 C:\
  • path.getNameCount() 返回路径中的 名称 元素数量(在这个例子中为 3
  • path.getName(0) 返回 datapath.getName(1) 返回 processed 等等...
  • path.subpath(fromInclusive, toExclusive) 返回一个相对路径,是该路径的 名称 元素的子序列。
  • path.relativize(path) 返回到参数路径的相对路径。

对象 Path 表示实际路径的 抽象。如果你想要将 \ 替换为 / 作为字符串,你可能需要使用 String::replace

String stringPath = path.toString().replace('\\', '/');
System.out.println(path);          // data\processed\Test.xml
System.out.println(stringPath);    // data/processed/Test.xml
英文:

You can use NIO.2 API and its objects Path and Paths which is a abstraction over a file system.

Path path = Paths.get("C:\\data\\processed\\Test.xml");

You can also get Path from File using File::toPath. Actually, you need to get all the names in the path:

File file = new File("C:\\data\\processed\\Test.xml");
Path path = file.toPath();
int count = path.getNameCount();                        // the count of names
path = path.subpath(0, count);                          // all the names

Alternatively (thanks to @Holger) using Path:relativize (you find a relative path to the root C:/ which is all the names.

File file = new File("C:\\data\\processed\\Test.xml");
Path path = file.toPath();
path = path.getRoot().relativize(path);

Here are some relevant methods:

  • path.getRoot() returns C:\
  • path.getNameCount() returns the number of name elements in the path (3 in this case)
  • path.getName(0) returns data, path.getName(1) returns processed etc...
  • path.subpath(fromInclusive, toExclusive) returns a relative Path that is a subsequence of the name elements of this path.
  • path.relativize(path) returns a relative path to a parameter.

The object Path represents an abstraction of the actual path. If you want to replace \ with / as a String, you might need to use String::replace.

String stringPath = path.toString().replace('\\', '/');

System.out.println(path);          // data\processed\Test.xml
System.out.println(stringPath);    // data/processed/Test.xml

答案2

得分: 0

以下是翻译好的内容:

这里是一个简短的回答:

File file = new File("c:\\tmp\\abc.txt");     
String filePath = file.getCanonicalPath();    
String str = filePath.replace('\\', '/');      
java.net.URI uri = new java.net.URI(str);    
uri.getPath();                                
英文:

Here is a short answer:

File file = new File("c:\\tmp\\abc.txt");  		//file =C:\tmp\abc.txt
String filePath= file.getCanonicalPath();		//path= C:\tmp\abc.txt
String str=filePath.replace('\\', '/');			//str= C:/tmp/abc.txt
java.net.URI uri= new java.net.URI(str);		//uri= C:/tmp/abc.txt
uri.getPath();									//uri.getPath() = /tmp/abc.txt

huangapple
  • 本文由 发表于 2020年9月11日 19:13:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63845959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定