package org.example.guessdigit;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;import org.example.io.FileIO;public class Settings {
public final static String file = ".GuessDigit";
public final static int[][] highscores = new int[3][5];
public final static String[][] players = new String[][]{
{"player", "player", "player", "player", "player"},
{"player", "player", "player", "player", "player"},
{"player", "player", "player", "player", "player"},
};

public static void load(FileIO files){
BufferedReader in = null;

try{
in = new BufferedReader(new InputStreamReader(files.readFile(file)));

// 读取分数
for(int i = 0; i < 3; i++){
for(int j = 0; j < 5; j++){
highscores[i][j] = Integer.parseInt(in.readLine());
}
}
// 读取玩家
for(int i = 0; i < 3; i++){
for(int j = 0; j < 5; j++){
players[i][j] = in.readLine();
}
}
}catch(IOException ex){
ex.printStackTrace();
}catch(NumberFormatException e){  //load方法里会有NumberFormatException
e.printStackTrace();
}finally{
try{
if(in != null)
in.close();
}catch(IOException e){
}
}
}

public static void save(FileIO files){
BufferedWriter out = null;
try{
out = new BufferedWriter(new OutputStreamWriter(
files.writeFile(file)));
for(int i = 0; i < 3; i++){
for(int j = 0; j < 5; j++){
out.write(highscores[i][j]);
out.write("\n");
}
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < 5; j++){
out.write(players[i][j]);
out.write("\n");
}
}
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
if(out != null)
out.close();
}catch(IOException e){
}
}
}

public static void addScore(int diff, String player, int score){
for(int i = 0; i < 5; i++){
if(score > highscores[diff - 3][i]){
for(int j = 4; j > i; j--){
highscores[diff - 3][j] = highscores[diff - 3][j - 1];
players[diff - 3][j] = players[diff - 3][j - 1];
}
highscores[diff - 3][i] = score;
players[diff - 3][i] = player;
break;
}
}
}
}
具体调用
Settings.addScore(diff, player, score);
Settings.save(file);
Setting.load(file);  // load方法里会有NumberFormatException

解决方案 »

  1.   

    哥们儿用windows的吧?我觉得是你写的时候:
    out.write("\n");
    应该用跨系统的方法:
    out.write("System.lineSeparator()“)
    因为windows的断行是\r\n,readline没那么智能
      

  2.   

    打错一个引号,嘛 you get it
      

  3.   

    我的代码是在Android环境下运行的,需要这么改吗
      

  4.   

    android好像不需要不过试试无妨。
    其他的我看不出问题来,只能设断点debugparseInt那句了,看in.readline出来的是啥
      

  5.   

    就是Integer.parseInt(in.readLine());这里报异常
      

  6.   

    改成
    String inLine = in.readLine();
    Integer.parseInt(inLine); // break here
    断在第二行,看抛出异常之前inLine是什么值?
      

  7.   

    照你的方法试了下,line里面居然是Â,以前从来没见过这个字符,求解释啊
      

  8.   

    原因是这样:
    你在这里
    out.write(highscores[i][j]);
                        out.write("\n");
    写入的是int,并不是String,而你读出来的时候却当做String,所以出现了乱码。写入前把int转化成String就行啦
      

  9.   

    感谢你的耐心回复,问题解决了,果然在写文件的时候根本就没想到要转化成String
      

  10.   

    感谢你的耐心回复,问题解决了,果然在写文件的时候根本就没想到要转化成String
    不客气