2015-06-15 478 views
1

我試圖去我的計算機中的一些文本文件,但我不斷得到這個異常,雖然路徑是正確的,該文件存在。 這裏是我的代碼:FileNotFoundException雖然文件存在

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) 
+0

你試圖打印帶有'的System.out.println的wordFile的絕對路徑(wordFile .getAbsolutePath())'?如果不是,請嘗試複製文件資源管理器中最後一條語句的輸出以檢查路徑是否存在。此外,您是否檢查過您有權讀取和寫入所需的文件? – Laurent

+1

我的賭注是''wordList.txt.txt「'作爲真正的文件名。 @ Ohad121你檢查過你的文件名是你認爲的嗎?沒有隱藏文件擴展名? – Tom

+0

你可以在Windows中打開文件嗎?看起來更像是一個Windows錯誤,而不是找不到文件的錯誤。 –

回答

0

您可以重命名文件?如果您從其他位置複製文件名並且文件名中包含不可見字符,這可以提供幫助。

+0

是的,我可以重命名文件 – Ohad121

2

當我複製你的代碼並試圖將它保存在Eclipse中。我得到了下面的錯誤

enter image description here

我由此推斷,雖然你的路徑看起來「d:\\ \\ IDC東西\\ wordList.txt」,但實際上它是not.So我做什麼, 只需輸入此行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"); 

file.separator

Character that separates components of a file path. This is "/" on UNIX and "\" on Windows.

+0

謝謝你,它的作品! – Ohad121

+0

噢,我的,這是正確的,在D:\''(我認爲是'U + 202A')之前,***是一個看不見的字符。 –

相關問題