Java.io.FileNotFoundException in existing file

I get this error when trying 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)

The file exists in the directory, but I still get this error. However, when I copy the same file in the Project src folder of the Eclipse workspace, this exception is not returned (although this method also creates the World_X.fr file in the bin folder).

What I'm actually trying to do is get the absolute location of the .jar file through this:

fileLocation = new String(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath());

And then I add "World_X.fr" to the fileLocation line, but this does not work. Please help me in this regard.

+5
source share
4 answers

You need to cancel the space %20. eg:.

fileLocation = new String(
    Main.class.getProtectionDomain().getCodeSource().getLocation().getPath())
    .replaceAll("%20", " ");
+8
source

URL file: File :

File file = new File(url.toURI());

/.

getPath() .

+15

Here is the solution for this, it will only work after JDK1.5,

try { f = new File("somePath".toURI().getPath()); } catch(Exception e) {}
+6
source

Try to leave% 20 and use regular spaces instead. Also, you use backslashes in your code, if you use backslashes, make sure you avoid them first.

0
source

All Articles