我的文件保存算法有问题。
error:
dec 11, 2012 4:56:18 PM tetris.FileIO loadHighscores
SEVERE: null
java.io.FileNotFoundException: file:\C:\Users\Koen\Dropbox\2TI\vgo\Eindwerk\Tetris\build\classes\tetris\LineHighscores.txt (De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:656)
at tetris.FileIO.loadHighscores(FileIO.java:44)
at tetris.FileIO.getLineScores(FileIO.java:31)
at tetris.FileIO.main(FileIO.java:69)
Exception in thread "main" java.lang.NullPointerException
at tetris.FileIO.loadHighscores(FileIO.java:49)
at tetris.FileIO.getLineScores(FileIO.java:31)
at tetris.FileIO.main(FileIO.java:69)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
我知道出了什么问题,这是一个小型俄罗斯方块游戏的保存算法。
文件保存在:
...\Eindwerk\Tetris\build\classes\tetris\LineHighscore.txt ...\Eindwerk\Tetris\build\classes\tetris\TimeHighscore.txt
我在这个类中添加了一个主类。将其粘贴到您的 ide 中并运行它,我收到很多错误
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().getResource("LineHighscores.txt").toString());
}
public String[][] getTimeScores(){
return this.loadHighscores(this.getClass().getResource("TimeHighscores.txt").toString());
}
public String[][] loadHighscores(String path){
int x=0;
String test = "";
file = new File(path);
try {
filescScanner = new Scanner(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(file.toString());
while((test=filescScanner.nextLine())!=null) {
lineScanner = new Scanner(test);
lineScanner.useDelimiter("-/-");
System.out.println(lineScanner);
System.out.println(lineScanner.toString());
data[x][0]=lineScanner.next();//name
data[x][1]=lineScanner.next();//data
x++;
}
lineScanner.close();
filescScanner.close();
return data;
}
public static void main(String[] args){
FileIO file = new FileIO();
System.out.println(file.getLineScores());
}
}