当我尝试打开文件时出现此错误:
java.io.FileNotFoundException: D:\Portable%20Programs\Android%20Development\workspace3\XXX-desktop\bin\World_X.fr (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
该文件存在于目录中,但我仍然收到此错误。但是,当我在 Eclipse 工作区项目 src 文件夹中复制相同的文件时,没有返回此类异常(尽管此方法还在 bin 文件夹中创建了 World_X.fr 文件)。
我实际上想做的是通过这个获取 .jar 文件的绝对位置:
fileLocation = new String(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath());
然后我将“World_X.fr”附加到 fileLocation 字符串,但这不起作用。请在这方面帮助我。
最佳答案
将 file:
URL 转换为实际 File
的首选方法是:
File file = new File(url.toURI());
这会处理所有 检查和引用/转义。
改用 getPath()
会将这些奇怪的部分留给您。
关于现有文件上的 java.io.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11565694/