无法序列化java.util.HashMap对象

时间:2014-01-09 08:05:43

标签: java serialization input hashmap

我正在尝试将hashmap写入文件并将其读回。最初,我的文件是空的。所以,当我尝试使用readObject()读取时,它会抛出EOFException。我抓住它并继续正常执行。然后,我输入了学生的详细信息。使用rollnumber作为键在hashmap中插入详细信息。然后,我使用writeObject()将hashmap写入文件。我检查文件,里面写了一些东西。     我关闭程序并再次执行它。     这次它应该读取我写的hashmap。但是,它会清除文件并仍然抛出EOFException。     帮帮我..我该怎么办?

Student.java

package student;

public class Student implements java.io.Serializable {
    private String name;
    private int age;

    class Address implements java.io.Serializable{
        String house;
        String street;
        String city;
        String state;
        int pin;
        Address(String house,String street,String city,int pin,String state){
            this.house = new String(house);
            this.street = new String(street);
            this.city = new String(city);
            this.state = new String(state);
            this.pin = pin;
        }
    }
    private Address addr;
    private int rollnumber;
    private String courses[];

    public Student(String name,int age,String house,String street,String city,String state,int pin,int roll, String courses[]){
        this.name = new String(name);
        this.age = age;
        this.addr = new Address(house,street,city,pin,state);
        this.rollnumber = roll;
        this.courses = new String[4];
        System.arraycopy(courses, 0, this.courses, 0, 1);
        System.out.println("NEW STUDENT CREATED..");
    }

    public String toString(){
        String s = rollnumber +" "+name+" "+age+"\n";
        return s;
    }

    int getRollNumber(){
        return rollnumber;
    }

}

我已将两个类Student和内部类地址序列化。告诉我这里有什么问题。

StudentRunner.java

package student;
import java.io.*;
import java.util.*;

public class StudentRunner {
    public static void main(String [] args) throws java.io.IOException, ClassNotFoundException{
        String file = "student.txt";
        StudentProcessor sp = new StudentProcessor();
        FileOutputStream fout = new FileOutputStream(file);
        FileInputStream fin = new FileInputStream(file);
        ObjectOutputStream out = new ObjectOutputStream(fout);
        ObjectInputStream in = new ObjectInputStream(fin);

        HashMap<Integer, Student> hm= new HashMap<Integer, Student>();
        Student stud=null;
        try{
        hm = (HashMap<Integer, Student>)in.readObject();

                ///////// TROUBLE IN THE ABOVE LINE ///////////


        }catch(EOFException eof){
            System.out.println("sfdasfasfas");
        }

        Scanner scanner = new Scanner(System.in);
        int option=0;

        do{
            System.out.println("(1) Add User Details");
            System.out.println("(2) Display User Details");
            System.out.println("(3) Delete User Details");
            System.out.println("(4) Save User Details");
            System.out.println("(5) Exit");
            try{
                option = scanner.nextInt();
            }catch(InputMismatchException ime){
                System.out.println("Error!!! provide valid option(1-5).");
                continue;
            }


        switch(option){
            case 1:
                try{

                    scanner.nextLine();

                    System.out.println("Enter full name: ");
                    String name = scanner.nextLine();
                    System.out.println("Enter age: ");
                    int age = scanner.nextInt();
                    System.out.println("Enter rollnumber: ");
                    int rollnumber = scanner.nextInt();

                    scanner.nextLine();

                    System.out.println("Enter Address:");
                    System.out.println("  Enter house number ");
                    String house_num = scanner.nextLine();
                    System.out.println("  Enter street: ");
                    String street = scanner.nextLine();
                    System.out.println("  Enter city: ");
                    String city = scanner.nextLine();
                    System.out.println("  Enter state: ");
                    String state = scanner.nextLine();
                    System.out.println("  Enter pin: ");
                    int pin = scanner.nextInt();

                    String courses[] = new String[4];
                    System.out.println("Enter four courses(A-F)");
                    System.out.println("  Enter first course: ");
                    courses[0] = scanner.next();
                    System.out.println("  Enter second course: ");
                    courses[1] = scanner.next();
                    System.out.println("  Enter third course: ");
                    courses[2] = scanner.next();
                    System.out.println("  Enter fourth course: ");
                    courses[3] = scanner.next();

                    boolean flag = Validator.validateStudent(name, age, rollnumber, house_num, street, city, 
                                                             state, pin, courses);

                    stud= new Student(name,age,house_num,street,city,state,pin,rollnumber,courses);
                    if(flag){
                        hm.put(rollnumber, stud );
                    }
                }catch(InputMismatchException ime){
                    System.out.println("Error!!! Provide numeric value..");
                    continue;
                }
                break;
            case 2:
                Set set = hm.entrySet();
                Iterator itr = set.iterator();
                while(itr.hasNext()){
                    Map.Entry<Integer, Student> m =(Map.Entry<Integer, Student>) itr.next();
                    System.out.println(m.getValue());
                }
                break;
            case 3:
                break;
            case 4:
                out.writeObject(hm);
                break;
            case 5:
                break;
            default:
                System.out.println("Error!!! provide valid option(1-5).");
                continue;
        }

        if(option>=5)
            break;

        }while(true);
    }
}
StudentRunner.java中的

hm.readObject()是我在阅读内容的地方。但它清除已经写入的内容并抛出EOFException。

1 个答案:

答案 0 :(得分:0)

请记住始终在您正在使用的close上调用OutputStream方法。这样做的公理惯例是在finally {}块中执行它,这样你就可以完全确定你正在关闭OutputStream(从而实际上将缓冲区刷新到磁盘中)