2016-12-08 83 views
0

我想写一个高分类到我正在制作的游戏中,所以我将玩家的名字和他的分数的值写入Score类,然后检查HighScore类如果它在高分中。 但是我在serialziation中遇到了一个简单的问题,但我不确定错误是什么。这是代码:Java序列化EOF异常

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Iterator; 

public class HighScore<T extends Serializable & Comparable<T>> implements 
     Serializable, Iterable<T> { 

    private int size = 3; 
    private ArrayList<T> list; 
    private static final String HS_FILE = "resources/High_Scores.txt"; 
    ObjectOutputStream oos = null; 
    ObjectInputStream ois = null; 
    public String name; 

    public HighScore(int size) { 
     this.size = size; 
     list = new ArrayList<>(size); 
    } 

    public boolean add(T val) throws IOException, ClassNotFoundException { 

     readFile(); 
     boolean addWorks = list.size() < size || list.get(list.size() 
       - 1).compareTo(val) < 0; 
     if (addWorks) { 
      list.add(val); 
      Collections.sort(list, Collections.reverseOrder()); 
      if (list.size() > size) { 
       list.remove(size); 
      } 
      updateFile(); 
      int i = 1; 
      for (Iterator<T> it = list.iterator(); it.hasNext();) { 
       Score n = (Score) it.next(); 
       System.out.println(i + ": " + n.name + ", " + n.score + " points "); 
       i++; 
      } 
     } 
     return addWorks; 
    } 

    private void readFile() throws IOException, ClassNotFoundException { 
     ois = new ObjectInputStream(new FileInputStream(HS_FILE)); 
     list = (ArrayList<T>) ois.readObject(); 
    } 

    private void updateFile() throws FileNotFoundException, IOException { 
     oos = new ObjectOutputStream(new FileOutputStream(HS_FILE)); 
     oos.writeObject(list); 
    } 

    @Override 
    public Iterator<T> iterator() { 
     return list.iterator(); 
    } 
} 

比分类如下:

import java.io.Serializable; 

public class Score implements Serializable, Comparable<Score> { 

String name; 
int score; 

public Score(String name, int score) { 
    this.name = name; 
    this.score = score; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public int getScore() { 
    return score; 
} 

public void setScore(int score) { 
    this.score = score; 
} 

@Override 
public String toString() { 
    return "Score{" + "name=" + name + ", score=" + score + '}'; 
} 

@Override 
public int compareTo(Score that) { 
    return this.score - that.score; 
} 

} 

的错误输出显示的是:

Dec 07, 2016 9:22:01 PM 
brachabee2.entities.Player gameOver 
SEVERE: null 
java.io.EOFException 

任何帮助将是巨大的!谢谢!

+0

你应该**使用后关闭**流。在这方面,您不应该在实例字段中存储对这些临时流的引用。因此,最简洁的用法是[The try-with-resources Statement](https://docs.oracle.com/javase/8/docs/technotes/guides/language/try-with-resources.html)。 – Holger

回答

0

我假设你试图序列化/反序列化Score?你可以检查它是否实现了Serializable?你可以通过分数代码

+0

好的,我编辑了代码并添加了分数,任何想法? – Jonathan

+0

是否存在“resources/High_Scores.txt”?这可能是因为您正在尝试在文件尚不存在时读取该文件 –