信息 :
当我执行我的程序时,一个简单的文件阅读器,我得到一个异常。
我的文件保存在 .java 文件旁边。
输出 :
运行:
名称:Koen
得分:44
线程“main”中的异常 java.util.NoSuchElementException:找不到行
名称:Kevin
得分:55
at java.util.Scanner.nextLine(Scanner.java:1585)
at tetris.FileIO.loadHighscores (FileIO.java:41)
at tetris.FileIO.getLineScores(FileIO.java:28)
at tetris.FileIO.main(FileIO.java:62)
Java 结果:1
构建成功(总时间:0 秒)
代码:
package tetris;
import java.io.File;
import java.io.InputStream;
import java.io.Writer;
import java.util.Scanner;
public class FileIO {
private File file;
private Scanner filescScanner, lineScanner;
private Writer fileWriter, lineWriter;
private String[][] data;
public FileIO () {
String[][] data = new String[100][1];
}
public String[][] getLineScores(){
return this.loadHighscores(this.getClass().getResourceAsStream("LineHighscores.txt"));
}
public String[][] getTimeScores(){
return this.loadHighscores(this.getClass().getResourceAsStream("TimeHighscores.txt"));
}
public String[][] loadHighscores( InputStream resourceStream){
int x=0;
String test = "";
filescScanner = new Scanner(resourceStream);
while((test=filescScanner.nextLine())!=null) {
lineScanner = new Scanner(test);
lineScanner.useDelimiter("-/-");
System.out.println("name : "+lineScanner.next());
System.out.println("score : " +lineScanner.next());
x++;
}
lineScanner.close();
filescScanner.close();
return data;
}
public static void main(String[] args){
FileIO file = new FileIO();
System.out.println(file.getLineScores());
}
}