问题
The following gives a leading slash before the disk name. How can I avoid that?
String pngpath = getClass().getResource("/resources/lena.png").getPath();
System.out.println("pngpath = "+pngpath);
Gives:
pngpath = /C:/Users/jgrimsdale/Documents/NetBeansProjects/HelloCV/build/classes/resources/lena.png
回答1:
Use:
String pngpath = getClass().getResource("/resources/lena.png").getFile();
File file = new File(pngpath);
System.out.println(file.getAbsolutePath());
回答2:
A constructor of File(uri) or File(string) helps to get file object from system dependent path string or URI object.
It is a solution to using the Java Library.
System.out.println(new File("/C:/Users/jgrimsdale").toString())
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.net.URI)
回答3:
you can do this using this code.
System.out.println("pngpath = "+pngpath.substring(1,pngpath.length()));
来源:https://stackoverflow.com/questions/15662820/getresource-puts-a-leading-before-the-disk-name-using-java-1-7-windows-7