这是我的hashMapMap<Integer,Student> students=new HashMap<Integer,Student>();当我向里面添加数据完了以后用:stuInfo = new File("stuinfo.ser");
oos = new ObjectOutputStream( new FileOutputStream(stuInfo));
oos.writeObject(students);
oos.close();将students序列化存在stuinfo.ser中没有问题,当我关闭程序重新读取时:ois= new ObjectInputStream(new FileInputStream(stuInfo));
students=(Map<Integer, Student>) ois.readObject();程序出现java.io.EOFException当我没有关闭程序时在反序列化就可以,这是为什么?怎么解决!我的目的是是想把在程序关闭时之前students保存下来,当下次打开程序再将它读取出来求指教!

解决方案 »

  1.   

    一般而言,读取次数超过写入次数时会发生EOFException,表明已到文件尾部,已无数据或对象可读。
    对称地写读不会发生这类异常。
      

  2.   

    如下是测试代码,没有问题,我是疑惑楼主代码或许有问题。import java.io.File;
    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.HashMap;
    import java.util.Map;public class Test {
    public static void main(String args[]) {
    writer();//自己可以注释掉代码试试
    reader();
    }
    public static void writer(){
    Map<Integer,Student> students=new HashMap<Integer,Student>();
    students.put(1, new Student("A"));
    students.put(2, new Student("B"));
    File stuInfo = new File("stuInfo.txt");
    ObjectOutputStream oos = null;
    try {
    oos = new ObjectOutputStream( new FileOutputStream(stuInfo));
    oos.writeObject(students);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }finally{
    try {
    oos.flush();
    oos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    public static void reader(){
    Map<Integer,Student> students= null;
    File stuInfo = new File("stuInfo.txt");
    ObjectInputStream ois = null;
    try {
    ois = new ObjectInputStream(new FileInputStream(stuInfo));
    Object o = ois.readObject();
    students = (Map<Integer,Student>)o;

    System.out.println(students);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }finally{
    try {
    ois.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    }
    class Student implements Serializable{
    Student(String name){
    this.name = name;
    }
    private String name;

    @Override
    public String toString(){

    return "Student name:"+name;
    }
    }
      

  3.   

    import java.io.File;
    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.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;/**
     * 用于测试HashMap的序列化问题
     * @author Evi
     */public class Test {
    File stuinfo;//存放hashmap序列化后的文件
    ObjectOutputStream oos = null;
    ObjectInputStream  ois = null;
        /**
         * 构造函数
         * @param stuinfo
         * File 存放HashMap<Integer,Student>
         */
    Test(File stuinfo){
    this.stuinfo=stuinfo;
    try {
    oos = new ObjectOutputStream(new FileOutputStream(stuinfo));
    ois = new ObjectInputStream(new FileInputStream(stuinfo));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    /**
     * 将sourstudents写入文件stuinfo.ser
     * @param sourstudents
     * Map<Integer,Student>
     */
    public void write(Map<Integer,Student> sourstudents){
    try {
    oos.writeObject(sourstudents);
    oos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
     * 从stuinfo.ser读取出数据存放在deststudents (HashMap<Integer,Student>)中
     * @return deststudents
     * Map<Integer,Student> 返回从stuinfo.ser文件中读取出来的deststudents
     */
    public Map<Integer,Student> read(){
    Map<Integer,Student> deststudents = null;
    try {
    deststudents=(Map<Integer, Student>) ois.readObject();
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return deststudents;
    }

    /**
     * 显示students中存放的学生信息
     * @param sourstudents2
     * Integer,Student> students
     */
    public void display(Map<Integer, Student> students){
    Iterator<Entry<Integer,Student>> iter = students.entrySet().iterator();
    while (iter.hasNext()) {
    Map.Entry<Integer, Student> entry = iter.next();
    Student stu = entry.getValue();
    System.out.println("  学号:" + stu.getId() + "  姓名:"+ stu.getName());
    }
    } public static void main(String[] args) {
    File stuinfo=new File("stuinfo.ser");
    //由stuinfo.ser读取出来的HashMap<Integer,Student>
    Map<Integer,Student> deststudents=new HashMap<Integer,Student>();
    /*------------初始化一个HashMap<Integer,Student>----------------*/
    Map<Integer,Student> sourstudents=new HashMap<Integer,Student>();
    Student stu1 = new Student(Integer.valueOf(3),"张三");
    Student stu2 = new Student(Integer.valueOf(8),"李四");
    sourstudents.put(stu1.getId(), stu1);
    sourstudents.put(stu2.getId(), stu2);
    Test test=new Test(stuinfo);

    /*-----------第一次执行的代码-----------------*/
    //显示students中的学生信息
    test.display(sourstudents);
    //将初始化好的students (HashMap<Integer,Student>)写入stuinfo.ser文件中
    test.write(sourstudents);

    /*-------------第二次执行的代码---------------*/
    //从stuinfo.ser文件中读数据将其存放在test对象的deststudents(HashMap<Integer,Student>)中
    deststudents=test.read();
    test.display(deststudents);

    /*
     * 说明:
     * 第一次运行程序将第二次执行的代码注释,执行第一次执行的代码 程序正常运行 
     *  --- 这时已经将sourstudents写入文件stuinfo.ser中
     *  
     *  
     * 第二次运行程序注释第一次执行的代码执行第二次的代码程序报错java.io.EOFException
     * ----从stuinfo.ser读取数据报错为什么
     * 
     * 
     * 程序一次运行两段代码正常
     */


    }}
    这是我简化后的代码
      

  4.   

    你真狠看看你的构造函数:
    Test(File stuinfo){
      this.stuinfo=stuinfo;
      try {
      oos = new ObjectOutputStream(new FileOutputStream(stuinfo));直接就用把FileOutputStream这个文件的内容清空了
      

  5.   

    原来是这样的啊,学习了
    感谢!感谢!每次出问题都是CSDN上给解决的,最后祝各位身体健康,合家幸福!