2013-12-09 75 views
2

我得到一個FileNotFoundException。但該文件存在。FileNotFoundException當文件存在時

下面是代碼:

try { 
     FileInputStream fis = new FileInputStream("conf/sampleprop.conf"); 
     Properties prop = new Properties(); 
     prop.load(fis); 
     String file = prop.getProperty("FILE"); 
     System.out.println("File : " + file); 



     BufferedReader reader = new BufferedReader(new FileReader(file)); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      System.out.println(line); 
     } 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

,輸出是:

File : "C:\Chithra\Contacts.txt" 
java.io.FileNotFoundException: "C:\Chithra\Contacts.txt" (The filename, directory name, or volume label syntax is incorrect) 
at java.io.FileInputStream.open(Native Method) 
at java.io.FileInputStream.<init>(Unknown Source) 
at java.io.FileInputStream.<init>(Unknown Source) 
at java.io.FileReader.<init>(Unknown Source) 
at PropertiesTest.main(PropertiesTest.java:32) 

但文件實際上退出, 誰能幫助請

+1

這是在Windows上運行嗎? –

+0

是在Windows中使用eclipse – user1407668

+0

給出完整代碼,包括「文件」的初始化 – TheLostMind

回答

-1

嘗試設置文件名是 「C:/Chithra/Contacts.txt」

不要在Java中使用 「\」,java的讀取與分離文件路徑「/」

相關問題