java – 如何避免从URL.getFile()获取URL编码路径?
发布时间:2020-05-24 12:31:28 所属栏目:Java 来源:互联网
导读:我在尝试获取给定资源的路径时遇到以下问题: System.out.println(nf=+new File(.).getAbsolutePath()); System.out.println(od=+new File(this.getClass().getResource(.).getFile()); 我得到的输出是: nf=C:Userscurrent u
|
我在尝试获取给定资源的路径时遇到以下问题: System.out.println("nf="+new File(".").getAbsolutePath());
System.out.println("od="+new File(this.getClass().getResource(".").getFile());
我得到的输出是: nf=C:Userscurrent userworkspacexyz. od=C:Userscurrent%20userworkspacexyzbinsomething 问题在于URL编码事物.怎么避免呢?有没有直接的方法来避免首先获得这种字符串,或者我应该只针对一些将进行URL解码的方法运行返回的字符串? 谢谢 解决方法这是由于API中的URL处理怪癖.您可以通过首先将URL字符串转换为URI来解决此问题:new URI(this.getClass().getResource(".").toString()).getPath()
这将生成一个String,如下所示: "C:Userscurrent userworkspacexyzbinsomething" (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
