简体   繁体   English

文件存在时发生FileNotFoundException

[英]FileNotFoundException when file exists

I am getting a FileNotFoundException. 我收到FileNotFoundException。 But the file exists. 但是该文件存在。

Below is the code: 下面是代码:

 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();
     }

and the output is: 输出为:

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)

But the file actually exits, Can anyone help please 但是文件实际上已经退出,有人可以帮忙吗

Maybe you don't have the permission to read this file. 也许您没有读取此文件的权限。 See this post: 看到这篇文章:

Get FileNotFoundException when initialising FileInputStream with File object 使用File对象初始化FileInputStream时获取FileNotFoundException

Try to set the file name to be "C:/Chithra/Contacts.txt" 尝试将文件名设置为“ C:/Chithra/Contacts.txt”

Do not use "\\" in java, java reads file paths separated with "/" 在Java中不要使用“ \\”,Java会读取以“ /”分隔的文件路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM