有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中通过序列化获取多个对象

我想使用序列化从文件中获取Employee类的多个对象。在这里,我只得到一个对象,任何解决方案

    package com.jbt;

    import java.io.Serializable;

    public class Employee implements Serializable
    {
       public String firstName;
       public String lastName;
       private static final long serialVersionUID = 5462223600l;
    }




    package com.jbt;

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;

    public class SerializaitonClass {

        public static void main(String[] args) {
            Employee emp = new Employee();
            emp.firstName = "Vivekanand";
            emp.lastName = "Gautam";

            try {
                FileOutputStream fileOut = new FileOutputStream("./employee.txt");
                ObjectOutputStream out = new ObjectOutputStream(fileOut);
                out.writeObject(emp);
                out.close();
                fileOut.close();
                System.out.printf("Serialized data is saved in ./employee.txt file");
            } catch (IOException i) {
                i.printStackTrace();
            }
        }
    }



package com.jbt;

import java.io.*;

public class DeserializationClass {
    public static void main(String[] args) {
        Employee emp = null;
        try {
            FileInputStream fileIn = new FileInputStream("./employee.txt");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            emp = (Employee) in.readObject();
            in.close();
            fileIn.close();
        } catch (IOException i) {
            i.printStackTrace();
            return;
        } catch (ClassNotFoundException c) {
            System.out.println("Employee class not found");
            c.printStackTrace();
            return;
        }
        System.out.println("Deserializing Employee...");
        System.out.println("First Name of Employee: " + emp.firstName);
        System.out.println("Last Name of Employee: " + emp.lastName);
    }
}

在反序列化类中,我如何在列表中获取多个对象?? 提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    List<Employee> employeeList = new ArrayList<Employee>();
    FileInputStream fis = null;
    ObjectInputStream in = null;
    try {
        fis =new FileInputStream("./employee.txt");
        in = = new ObjectInputStream(fis);
        while (true) {
            employeeList.add((Employee) in.readObject());
        }
    } catch (EOFException eof) {
        // Ignore. means you reached EOF
    } finally {
        if (fis != null)
            fis.close(); // close the stream
        if(in != null)
            in.close();
    }
    

    注意:您还可以序列化List并将其存储在文件中,然后对其进行反序列化。(添加列表为可序列化列表)

  2. # 2 楼答案

    可以按如下方式序列化和反序列化对象数组:

    File file = new File("out.ser");
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
    
            //Create the multiple Objects
            SerializeMe serializeMe = new SerializeMe(1);
            SerializeMe serializeMe1 = new SerializeMe(10);
            SerializeMe serializeMe2 = new SerializeMe(100);
            SerializeMe serializeMe3 = new SerializeMe(1000);
    
            //Create an array and assign objects
            Object[] obj=new Object[]{serializeMe,serializeMe1,serializeMe2,serializeMe3};
            // Write object array to Stream Class
            oos.writeObject(obj);
            oos.close();
    
            //Process of Deserializable
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
    
            //create an array of Object class
            Object[] obj1=(Object[]) ois.readObject();
    
            //looping the array
            for(int i=0;i<obj1.length;i++){
    
                SerializeMe dto = (SerializeMe) obj1[i];
                System.out.println("data : " + dto.getData());
            }
            ois.close();