java.io.WriteAbortedException:写入中止;问题

java.io.WriteAbortedException: writing aborted; Issue

在使用序列化保存我的游戏中获得的高分时遇到问题 我在命令行中 运行 jar 时得到的错误是 java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream 我认为这之前是有效的,不知道为什么现在有问题

可能是因为我有一个 InputStream,我也得到 java.io.NotSerializableException: java.io.BufferedInputStream

InputStream is_game_over = this.getClass().getResourceAsStream("/Snake/lib/font/game_over.ttf");
InputStream is_Premier = this.getClass().getResourceAsStream("/Snake/lib/font/Premier2019-rPv9.ttf");

GamePanel() {
    try {
        GamePanel g = rescueGame("/temp/SnakeGameInformation.ser");
        HIGH_SCORE = g.HIGH_SCORE;
    } catch(Exception e) {
        e.printStackTrace();
    }
        
    ...
}

public void gameOver(Graphics g) {
    ...

    //Updates the High Score
    if(applesEaten > HIGH_SCORE) {  
        HIGH_SCORE = applesEaten;
        highScoreDisplay(g, true);
    } else {
        highScoreDisplay(g, false);
    }

    //Save the game information before closing
    try {
        saveGame("/temp/SnakeGameInformation.ser");
    } catch(Exception e) {
        e.printStackTrace();
    }

     ...
}

public void saveGame(String file) throws IOException {
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
    out.writeObject(this);
    out.close();
}

public GamePanel rescueGame(String file) throws IOException, ClassNotFoundException {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
    GamePanel g = (GamePanel)in.readObject();
    in.close();
    return g;
}

错误是我在声明

InputStream is_game_over = this.getClass().getResourceAsStream("/Snake/lib/font/game_over.ttf");
InputStream is_Premier = this.getClass().getResourceAsStream("/Snake/lib/font/Premier2019-rPv9.ttf");

在 class 上,所以它会被序列化,但它不能,所以我只是在构造函数中创建了变量,所以它不应该被序列化并解决了它!

代码应该是这样的

GamePanel() {
    try {
        InputStream is_game_over = this.getClass().getResourceAsStream("/Snake/lib/font/game_over.ttf");
        InputStream is_Premier = this.getClass().getResourceAsStream("/Snake/lib/font/Premier2019-rPv9.ttf");
        GamePanel g = rescueGame("/temp/SnakeGameInformation.ser");
        HIGH_SCORE = g.HIGH_SCORE;
    } catch(Exception e) {
        e.printStackTrace();
    }
        
    ...
}

public void gameOver(Graphics g) {
    ...

    //Updates the High Score
    if(applesEaten > HIGH_SCORE) {  
        HIGH_SCORE = applesEaten;
        highScoreDisplay(g, true);
    } else {
        highScoreDisplay(g, false);
    }

    //Save the game information before closing
    try {
        saveGame("/temp/SnakeGameInformation.ser");
    } catch(Exception e) {
        e.printStackTrace();
    }

     ...
}

public void saveGame(String file) throws IOException {
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
    out.writeObject(this);
    out.close();
}

public GamePanel rescueGame(String file) throws IOException, ClassNotFoundException {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
    GamePanel g = (GamePanel)in.readObject();
    in.close();
    return g;
}