继续获取StreamCorruptedException,即使代码很好

时间:2015-05-17 22:17:21

标签: java eclipse

FileInputStream fis = new FileInputStream(dataFile);
    ObjectInputStream getItem = new ObjectInputStream(fis);
    player[0] = (HighScore) getItem.readObject();

    for(int i = 0; i< player.length; i++)
    {
        highscoreinfo.setText("<html>" + highscoreinfo.getText() + "<br>" + player[i]);
        System.out.println(player[i]);

    }

    getItem.close();

如上所示,播放器[0]行是给我错误的。即使我在循环中有这个,它也会给我同样的错误。请记住,数据文件中只有一行数据,这就是为什么我只想让控制台读取第一行 编辑:以下是代码的编写部分,正如您所希望的那样:

FileOutputStream fos = new FileOutputStream(dataFile, true);
    ObjectOutputStream saveItem = new ObjectOutputStream(fos);  
    player[intplayernum] = new HighScore(userName, timePlayed, totalwords, timecount.getText());
    saveItem.writeObject(player[intplayernum].toString());

    for(int i = 0; i<= intplayernum; i++)
    {
        System.out.println(player[i]);
    }

    System.out.println("Player score written to file.");
    saveItem.close();
     GameFrame.setVisible(false);
     GameFrame.dispose();
     askNameApp();
     intplayernum++;

0 个答案:

没有答案