【问题标题】:FileNotFoundException when file exists文件存在时的 FileNotFoundException
【发布时间】:2013-12-09 08:27:20
【问题描述】:

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

但是文件实际上已经退出了, 谁能帮忙

【问题讨论】:

  • 这是在 Windows 上运行的吗?
  • 在 Windows 中使用 eclipse 是的
  • 给出完整的代码,包括“文件”的初始化
  • 为什么你的文件名用双引号括起来?
  • 删除双引号并将其作为 FILE = C:\Chithra\Contacts.txt 存储在属性文件中。属性值不应在双引号中使用

标签: java eclipse filenotfoundexception


【解决方案1】:

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

Get FileNotFoundException when initialising FileInputStream with File object

【讨论】:

  • 但我在 Windows 机器上使用 eclipse 运行并创建了 txt 文件
  • 尝试使用 File 类中的 canRead() 和 isFile() 方法。请参阅提供的链接中的第一个答案。
  • 你在写完文件后关闭了你的作家吗?
【解决方案2】:

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

java中不要使用“\”,java读取文件路径用“/”隔开

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-21
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    相关资源
    最近更新 更多