Raf*_*fay 7 java filenotfoundexception java-io
我尝试打开文件时收到此错误:
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)
Run Code Online (Sandbox Code Playgroud)
该文件存在于目录中,但我仍然收到此错误.然而,当我复制相同的文件在Eclipse工作区项目src文件夹,没有这样的例外是返回(尽管这种方法也bin文件夹创建World_X.fr文件).
我实际上要做的是通过这个获取.jar文件的绝对位置:
fileLocation = new String(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath());
Run Code Online (Sandbox Code Playgroud)
然后我将"World_X.fr"附加到fileLocation字符串,但这不起作用.请帮助我这方面.
A.H*_*.H. 17
将file:URL转换为实际的首选方法File是:
File file = new File(url.toURI());
Run Code Online (Sandbox Code Playgroud)
这将负责所有检查和报价/转义.
getPath()相反,使用会将这些奇数位留给您.
你需要%20将空间转移到空间.例如:
fileLocation = new String(
Main.class.getProtectionDomain().getCodeSource().getLocation().getPath())
.replaceAll("%20", " ");
Run Code Online (Sandbox Code Playgroud)
这是解决方案,这只适用于JDK1.5之后,
try { f = new File("somePath".toURI().getPath()); } catch(Exception e) {}
Run Code Online (Sandbox Code Playgroud)