How to avoid getting URL encoded paths from URL.getFile()?

我的梦境 提交于 2019-12-01 15:33:09

问题


I'm having the following problem when trying to get the path of a given resource:

    System.out.println("nf="+new File(".").getAbsolutePath());      
    System.out.println("od="+new File(this.getClass().getResource(".").getFile());

The output I get is:

nf=C:\Users\current user\workspace\xyz\.
od=C:\Users\current%20user\workspace\xyz\bin\something

The problem lies with the %20 URL encoding thing. How to avoid it? Is there a direct way to avoid getting this kind of string in the first place, or should I just run the returned string against some method that will do the URL decoding?

Thanks


回答1:


This is due to a URL handling quirk in the API. You can work around this by converting the URL string to a URI first:

new URI(this.getClass().getResource(".").toString()).getPath()

This will produce a String as follows:

"C:\Users\current user\workspace\xyz\bin\something"


来源:https://stackoverflow.com/questions/8928661/how-to-avoid-getting-url-encoded-paths-from-url-getfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!