import java.io.*;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) throws FileNotFoundException {
File test = new File("test.txt");
Scanner read = new Scanner (test);
String input;
input = read.next();
System.out.println (input);
}
}
当我运行此代码时,我收到此错误
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>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Test.main(Test.java:9)
我已经创建了文本文件,它就在项目文件夹中,但没有找到它。
编辑
InputStream file = getClass().getResourceAsStream("highScore.txt");
BufferedReader br = new BufferedReader (new InputStreamReader(file,"UTF-8"));
String text;
text = br.readLine();
while (text!=null) {
System.out.println (text);
}
我试过了,但我得到了 NullPointerException 错误
这是一个小程序。