Java – how to avoid from URL Getfile() get URL encoding path?
•
Java
I encountered the following problems while trying to get the path to the 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 is URL encoding things How to avoid it? Is there a direct way to avoid getting this string first, or should I only run the returned string for some methods that will decode the URL?
thank you
Solution
This is due to URL handling quirks in the API You can solve this problem by first converting the URL string to a URI:
new URI(this.getClass().getResource(".").toString()).getPath()
This will generate a string as follows:
"C:\Users\current user\workspace\xyz\bin\something"
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码