簡體   English   中英

文件存在時發生FileNotFoundException

[英]FileNotFoundException when file exists

我收到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)

但是文件實際上已經退出,有人可以幫忙嗎

也許您沒有讀取此文件的權限。 看到這篇文章:

使用File對象初始化FileInputStream時獲取FileNotFoundException

嘗試將文件名設置為“ C:/Chithra/Contacts.txt”

在Java中不要使用“ \\”,Java會讀取以“ /”分隔的文件路徑

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM