另一个序列化问题

时间:2016-05-13 19:10:22

标签: java serialization

我试图反序列化一系列分数。我花了一整天时间在这里搜索但找不到解决方案..我的代码看起来像这样:

public class Score implements Comparable<Score>, Serializable {
    private String name;
    private int score;
    // .......

}

public class MySortedList<T> extends...implements...,Serializable {

    private ArrayList<T> list;
    // ....
}

public class ScoreManager {

    private final String FILEPATH;
    private final String FILENAME = "highscores.ser";

    private MySortedList<Score> scoreList;

    public ScoreManager() {

        File workingFolder = new File("src\\games\\serialized");
        if (!workingFolder.exists()) {
            workingFolder.mkdir();
        }
        FILEPATH = workingFolder.getAbsolutePath();

        if ((scoreList = loadScores()) == null) {
            scoreList = new MySortedList<Score>();
        }
    } 

    public void addScore(Score score) {
        scoreList.add(score);
        saveScores();
    }

    public MySortedList<Score> getScoreList() {
        return scoreList;
    }

   private void saveScores() {
       try (ObjectOutputStream out = new ObjectOutputStream(new   FileOutputStream(new File(FILEPATH, FILENAME)))) {
        out.writeObject(scoreList);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
 }

@SuppressWarnings("unchecked")
private MySortedList<Score> loadScores() {
    try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(FILEPATH, FILENAME)))) {
        return (MySortedList<Score>) in.readObject();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
}

loadScores()方法每次只返回一个空的MySortedList。 但是程序成功地在正确的位置创建了highscores.ser文件,我绝对没有错误。分数对象正确添加到MySortedList对象。

有什么想法吗?也许值得一提的是,这是Swing制作的更大程序的一部分。当玩家死亡时,会调用ScoreManager类中的方法

1 个答案:

答案 0 :(得分:-1)

只有它可以提供帮助,这段代码对我有用:

class Score implements Comparable<Score>, Serializable{
    private int point;
    public Score(int point) {
        this.point = point;
    }
    public int getPoint(){
        return point;
    }
    @Override
    public int compareTo(Score o) {
        if (o.getPoint() == this.getPoint()) 
            return 0;
        return this.point < o.getPoint() ? - 1 : 1;
    }
    public String toString() {
        return "points: " + point;
    }

}
class MyList<T> implements Serializable {
    private ArrayList<T> list = new ArrayList<>();
    public void add(T e){
        list.add(e);
    }
    public void show() {
        System.out.println(list);
    }
}

public class Main {
    File workingFolder;
    String FILEPATH;
    private final String FILENAME = "highscores.ser";


    MyList<Score> list = new MyList<>();

    public static void main(String[] args) {

        Main main = new Main();
        main.createFolder();
        main.addItems();
        main.saveScores();
        MyList<Score> tempList = main.loadScores();
        tempList.show();
        main.addMoreItems();
        main.saveScores();
        tempList = main.loadScores();
        tempList.show();
    }
    private void addItems() {
        Score sc = new Score(10);
        list.add(sc);
    }
    private void addMoreItems() {
        Score sc1 = new Score(20);
        list.add(sc1);
        Score sc2 = new Score(30);
        list.add(sc2);
    }
    private void createFolder() {
        workingFolder = new File("src\\games\\serialized");
        if (!workingFolder.exists()) {
            workingFolder.mkdir();
        }
        FILEPATH = workingFolder.getAbsolutePath();
    }

    private void saveScores() {
        System.out.println("before save: " + list);
           try (ObjectOutputStream out = new ObjectOutputStream(new   FileOutputStream(new File(FILEPATH, FILENAME)))) {
            out.writeObject(list);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    private MyList<Score> loadScores() {
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(FILEPATH, FILENAME)))) {
            return (MyList<Score>) in.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}