FileNotFoundException虽然该文件存在

时间:2015-06-15 19:19:03

标签: java file path filenotfoundexception randomaccessfile

我正在尝试在我的计算机中找到一些文本文件,但我仍然遇到此异常,尽管路径正确并且文件存在。 这是我的代码:

    public static void main(String[] args) throws IOException {

    File wordFile = new File("‪D:\\IDC\\Stuff\\wordList.txt");
    RandomAccessFile wordsList = new RandomAccessFile(wordFile, "rw");

    System.out.println(wordFile.exists());


}

错误:

Exception in thread "main" java.io.FileNotFoundException: ‪D:\IDC\Stuff\wordList.txt (The filename, directory name, or volume label syntax is incorrect)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
at WordChecker.main(WordChecker.java:12)

2 个答案:

答案 0 :(得分:2)

当我复制代码并尝试将其保存在Eclipse中时。我收到了以下错误

enter image description here

我从中得出结论,虽然你的路径看起来像D:\\ IDC \\ Stuff \\ wordList.txt&#39;但实际上并非如此。所以我做了什么,只需输入此行 File file =new File("D:\\IDC\\Stuff\\wordList.txt"); ,而不是从代码中复制并且它有效。似乎你也从某个地方复制了它,并且对于编码问题你会遇到问题。

还有一点,您应该使用System.getProperty("file.separator")代替\\或/,就像下面的

一样
File wordFile = new File("‪D:" + System.getProperty("file.separator")
                + "IDC" + System.getProperty("file.separator") + "Stuff"
                + System.getProperty("file.separator") + "wordList.txt");
  

<强>文件分割符

     

分隔文件路径组件的字符。这是&#34; /&#34;在UNIX和&#34; \&#34;在Windows上。

答案 1 :(得分:0)

您可以重命名该文件吗?如果您从其他位置复制文件名并且其中包含不可见的字符,这可能会有所帮助。