现有文件上的java.io.FileNotFoundException [英] java.io.FileNotFoundException on an existing file
问题描述
尝试打开文件时出现此错误:
I am getting this error when I try to open a file:
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工作区Project src文件夹中复制相同文件时,不会返回此类异常(尽管此方法还在bin文件夹中创建World_X.fr文件).
The file is existing in the directory but I am still getting this error. However when I copy the same file in the Eclipse workspace Project src folder, no such Exception is returned (though this method also creates the World_X.fr file in the bin folder).
我实际上想做的是通过以下方式获取.jar文件的绝对位置:
What I am actually trying to do is get the absolute location of the .jar file through this:
fileLocation = new String(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath());
然后我将"World_X.fr"附加到fileLocation字符串,但这不起作用.请在这方面帮助我.
And then I am appending "World_X.fr" to the fileLocation string but this is not working. Please help me in this regard.
推荐答案
您需要将%20
取消转义为空格.例如:
You need to unescape the %20
to spaces. e.g.:
fileLocation = new String(
Main.class.getProtectionDomain().getCodeSource().getLocation().getPath())
.replaceAll("%20", " ");
这篇关于现有文件上的java.io.FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!