我在尝试获取给定资源的路径时遇到以下问题:
System.out.println("nf="+new File(".").getAbsolutePath());
System.out.println("od="+new File(this.getClass().getResource(".").getFile());
我得到的输出是:
nf=C:\Users\current user\workspace\xyz\.
od=C:\Users\current%20user\workspace\xyz\bin\something
问题在于 %20 URL 编码。如何避免?有没有一种直接的方法可以避免首先获得这种字符串,或者我应该只针对将进行 URL 解码的某些方法运行返回的字符串?
谢谢
最佳答案
这是由于 API 中的 URL 处理怪癖。您可以通过首先将 URL 字符串转换为 URI 来解决此问题:
new URI(this.getClass().getResource(".").toString()).getPath()
这将产生如下字符串:
"C:\Users\current user\workspace\xyz\bin\something"
关于java - 如何避免从 URL.getFile() 获取 URL 编码路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928661/