【发布时间】:2014-11-13 22:26:45
【问题描述】:
我有一个问题,我无法创建一个正常的文件! 这是我的代码:
public class test {
public static void main(String[] args) throws Exception {
File myFile = new File("word.txt");
System.out.println("Attempting to read from file in: "+myFile.getCanonicalPath());
Scanner input = new Scanner(myFile);
String in = "";
in = input.nextLine();
}
}
我有这个错误:
Attempting to read from file in: C:\Users\AHMED\workspace\Video\word.txt
Exception in thread "main" java.io.FileNotFoundException: word.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at test.main(test.java:11)
【问题讨论】:
-
...那个文件存在吗?
-
这个错误有什么难以理解的地方?您正在尝试从一个不存在的文件中读取,那么除了异常之外您还期望什么?
-
好吧,我想这不是通过尝试读取文件来创建文件的“正常”方式。搜索有关使用 Java 创建文件的千篇一律的教程。
-
我很好奇他创建了文件并且他想从同一个文件中读取。为什么没有创建文件?
-
我真的不知道!我正在使用 Sphinx 项目,但我注意到它无法读取任何目录......所以我只创建一个新文件来测试它,但它不起作用......