掃描器類無法找到我用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