文件存在时的FileNotFoundException

时间:2013-12-09 08:27:21

标签: java eclipse 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)

但文件确实退出了, 任何人都可以帮忙吗

2 个答案:

答案 0 :(得分:1)

也许您没有阅读此文件的权限。看这篇文章:

Get FileNotFoundException when initialising FileInputStream with File object

答案 1 :(得分:-1)

尝试将文件名设置为“C:/Chithra/Contacts.txt”

不要在java中使用“\”,java读取用“/”

分隔的文件路径