如何避免从URL.getFile()获取URL编码路径?
我在尝试获取给定资源的路径时遇到以下问题:
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()
这将生成一个String,如下所示:
"C:\Users\current user\workspace\xyz\bin\something"