java.io.FileNotFoundException on an existing file

后端 未结 5 1557
离开以前
离开以前 2021-02-15 17:47

I am getting this error when I try to open a file:

java.io.FileNotFoundException: D:\\Portable%20Programs\\Android%20Development\\workspace3\\XXX-desktop\\bin\\W         


        
相关标签:
5条回答
  • 2021-02-15 17:52

    Try leaving out the %20, and use normal spaces instead. Also, you're using backslashes, in your code if you're using backslashes make sure you escape them first.

    0 讨论(0)
  • 2021-02-15 17:53

    The preferred way to convert a file: URL into an actual File is this:

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

    This takes care of all checks and quoting/escaping.

    Using getPath() instead will leave these odd bits up to you.

    0 讨论(0)
  • 2021-02-15 17:58

    You need to unescape the %20 to spaces. e.g.:

    fileLocation = new String(
        Main.class.getProtectionDomain().getCodeSource().getLocation().getPath())
        .replaceAll("%20", " ");
    
    0 讨论(0)
  • 2021-02-15 18:07

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

    try { f = new File("somePath".toURI().getPath()); } catch(Exception e) {}
    
    0 讨论(0)
  • 2021-02-15 18:07

    The confirmed solution is quite old and even though it works for this particular case it is far more convenient to use URLDecoder, because %20 is only one encoded character, but in your path there can be whole lot of different encoded characters.

    fileLocation = URLDecoder.decode(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8");
    
    0 讨论(0)
提交回复
热议问题