getResource puts a leading / before the disk name using java 1.7 windows 7

时光毁灭记忆、已成空白 提交于 2019-12-06 17:22:27

问题


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

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