Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法序列化java.util.HashMap对象_Java_Serialization_Input_Hashmap - Fatal编程技术网

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

无法序列化java.util.HashMap对象,java,serialization,input,hashmap,Java,Serialization,Input,Hashmap,我正在尝试将hashmap写入文件并将其读回。最初,我的文件是空的。因此,当我尝试使用readObject()读取时,它抛出EOFEException。我抓住了它,继续正常执行。然后,我输入一个学生的详细信息。使用rollnumber作为键在hashmap中插入详细信息。然后,我使用writeObject()将hashmap写入该文件。我检查了一下文件,里面写了一些东西。 我关闭程序并再次执行它。 这次它应该读我写的hashmap。但是,它会清除文件并仍然抛出EOFEException。 帮助我

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

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;
    }

}
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

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;
    }

}
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);
    }
}
package学生;
导入java.io.*;
导入java.util.*;
公共班级学生跑步者{
publicstaticvoidmain(字符串[]args)抛出java.io.IOException、ClassNotFoundException{
字符串文件=“student.txt”;
StudentProcessor sp=新的StudentProcessor();
FileOutputStream fout=新的FileOutputStream(文件);
FileInputStream fin=新的FileInputStream(文件);
ObjectOutputStream out=新的ObjectOutputStream(fout);
ObjectInputStream in=新的ObjectInputStream(fin);
HashMap hm=新的HashMap();
学生stud=null;
试一试{
hm=(HashMap)in.readObject();
/////////上面的问题///////////
}捕获(EOFEException eof){
System.out.println(“sfdasfasfas”);
}
扫描仪=新的扫描仪(System.in);
int选项=0;
做{
System.out.println(“(1)添加用户详细信息”);
System.out.println(“(2)显示用户详细信息”);
System.out.println(“(3)删除用户详细信息”);
System.out.println(“(4)保存用户详细信息”);
系统输出打印项次(“(5)退出”);
试一试{
选项=scanner.nextInt();
}捕获(输入不匹配异常输入法){
System.out.println(“错误!!!提供有效选项(1-5)”;
持续
}
开关(选件){
案例1:
试一试{
scanner.nextLine();
System.out.println(“输入全名:”);
字符串名称=scanner.nextLine();
System.out.println(“输入年龄:”;
int age=scanner.nextInt();
System.out.println(“输入卷号:”);
int rollnumber=scanner.nextInt();
scanner.nextLine();
System.out.println(“输入地址:”);
System.out.println(“输入门牌号”);
字符串house_num=scanner.nextLine();
System.out.println(“进入街道:”);
String street=scanner.nextLine();
System.out.println(“输入城市:”;
字符串city=scanner.nextLine();
System.out.println(“输入状态:”);
字符串状态=scanner.nextLine();
System.out.println(“输入pin:”);
int pin=scanner.nextInt();
字符串课程[]=新字符串[4];
System.out.println(“输入四门课程(A-F)”);
System.out.println(“进入第一个课程:”);
courses[0]=scanner.next();
System.out.println(“进入第二课程:”);
courses[1]=scanner.next();
System.out.println(“进入第三个过程:”);
courses[2]=scanner.next();
System.out.println(“进入第四门课程:”);
courses[3]=scanner.next();
布尔标志=Validator.validateStudent(姓名、年龄、卷号、门牌号、街道、城市、,
州、pin、课程);
stud=新生(姓名、年龄、房屋数量、街道、城市、州、pin、卷号、课程);
国际单项体育联合会(旗){
hm.put(辊号、螺柱);
}
}捕获(输入不匹配异常输入法){
System.out.println(“错误!!!提供数值…”);
持续
}
打破
案例2:
Set=hm.entrySet();
迭代器itr=set.Iterator();
while(itr.hasNext()){
Map.Entry m=(Map.Entry)itr.next();
System.out.println(m.getValue());
}
打破
案例3:
打破
案例4:
out.writeObject(hm);
打破
案例5:
打破
违约:
System.out.println(“错误!!!提供有效选项(1-5)”;
持续
}
如果(选项>=5)
打破
}虽然(正确);
}
}

StudentRunner.java中的hm.readObject()是我阅读内容的地方。但它会清除已经写入的内容并抛出EOFEException

请记住,在您用于编写的
OutputStream
上,始终调用
close
方法。这样做的公认习惯用法是在finally{}块中进行,这样您就可以完全确保您正在关闭
OutputStream
(从而实际将缓冲区刷新到磁盘中)

这可能不是您的问题,但千万不要尝试一次多次打开同一个文件;结果充其量是无法预测的。相反,在需要之前打开文件,读或写,然后关闭。是。。问题不在于读取文件。可能是因为我同时打开了同一个文件进行读写。问题