以下是磁盘名称前面的前导斜杠。
我怎么能避免这种情况?
String pngpath = getClass().getResource("/resources/lena.png").getPath();
System.out.println("pngpath = "+pngpath);
得到:
pngpath = /C:/Users/jgrimsdale/Documents/NetBeansProjects/HelloCV/build/classes/resources/lena.png
使用:
String pngpath = getClass().getResource("/resources/lena.png").getFile();
File file = new File(pngpath);
System.out.println(file.getAbsolutePath());
File(uri)或File(string)的构造函数有助于从系统相关的路径字符串或URI对象中获取文件对象。
这是使用Java库的解决方案。
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)
您可以使用此代码执行此操作。
System.out.println("pngpath = "+pngpath.substring(1,pngpath.length()));