扫描器类无法找到我用NetBeansIDE文件 ,并test.txt的是在文件夹路径:d:\ NetBeans项目工程\的ReadFile的\ src \ ReadFile的\ test.txt的Scanner类的java文件未找到
在readfile.java存在的文件夹中。代码如下: 它生成文件未找到。
package readfile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) throws IOException , FileNotFoundException
{
Scanner scanner = new Scanner(new File("test.txt"));
while (scanner.hasNextLine())
System.out.println(scanner.nextLine());
}
}
输出: -
run:
Exception in thread "main" java.io.FileNotFoundException: test.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.util.Scanner.<init>(Scanner.java:636)
at readfile.ReadFile.main(ReadFile.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
感谢@托马斯 - nukiewics您的帮助一起。我将test.txt粘贴到主根文件夹而不是类文件夹中。它的工作 – 2012-07-08 17:58:48