Java 文件存在时FileNotFoundException
我收到一个FileNotFoundException。但是文件存在 代码如下:Java 文件存在时FileNotFoundException,java,eclipse,filenotfoundexception,Java,Eclipse,Filenotfoundexception,我收到一个FileNotFoundException。但是文件存在 代码如下: try { FileInputStream fis = new FileInputStream("conf/sampleprop.conf"); Properties prop = new Properties(); prop.load(fis); String file = prop.getProperty("FILE");
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)
文件:“C:\Chithra\Contacts.txt”
java.io.FileNotFoundException:“C:\Chithra\Contacts.txt”(文件名、目录名或卷标语法不正确)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(未知源)
位于java.io.FileInputStream。(未知源)
位于java.io.FileReader。(未知源)
位于PropertiesTest.main(PropertiesTest.java:32)
但是文件确实存在,
有人能帮忙吗也许你没有阅读此文件的权限。见此帖:
尝试将文件名设置为“C:/Chithra/Contacts.txt”
在java中不要使用“\”,java读取以“/”分隔的文件路径这在Windows上运行吗?在Windows中使用eclipsegive完整代码(包括“文件”的初始化)是的,为什么文件名用双引号括起来?删除双引号,并将其存储为属性文件中的file=C:\Chithra\Contacts.txt。属性值不应在双引号中使用,但我在Windows机器中使用eclipse运行,并使用canRead()和来自File类的isFile()方法创建了txt fileTry。请参阅提供的链接中的第一个答案。写入文件后是否关闭了编写器?