示例:序列化字符串ArrayList

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class ArrayListExample 
{
    public static void main(String[] args) throws Exception 
    {
        ArrayList<String> namesList = new ArrayList<String>();

        namesList.add("JackLi");
        namesList.add("BobRobert");
        namesList.add("Lucie");
        try 
        {
            FileOutputStream fos = new FileOutputStream("listData");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(namesList);
            oos.close();
            fos.close();
        } 
        catch (IOException ioe) 
        {
            ioe.printStackTrace();
        }
    }
}

示例: 反序列化ArrayList

反序列化字符串数组列表

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class ArrayListExample 
{
    public static void main(String[] args) throws Exception 
    {
        ArrayList<String> namesList = new ArrayList<String>();

        try 
        {
            FileInputStream fis = new FileInputStream("listData");
            ObjectInputStream ois = new ObjectInputStream(fis);
            namesList = (ArrayList) ois.readObject();
            ois.close();
            fis.close();
        } 
        catch (IOException ioe) 
        {
            ioe.printStackTrace();
            return;
        } 
        catch (ClassNotFoundException c) 
        {
            System.out.println("Class not found");
            c.printStackTrace();
            return;
        }

        //Verify list data
        for (String name : namesList) {
            System.out.println(name);
        }
    }
}

输出:

JackLi
BobRobert
Lucie

反序列化对象数组列表

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class ArrayListExample 
{
    public static void main(String[] args) throws Exception 
    {
        ArrayList<Employee> employees = new ArrayList<>();

        try 
        {
            FileInputStream fis = new FileInputStream("employeeData");
            ObjectInputStream ois = new ObjectInputStream(fis);
            employees = (ArrayList) ois.readObject();
            ois.close();
            fis.close();
        } 
        catch (IOException ioe) 
        {
            ioe.printStackTrace();
            return;
        } 
        catch (ClassNotFoundException c) 
        {
            System.out.println("Class not found");
            c.printStackTrace();
            return;
        }

        //Verify list data
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }
}

输出:

Employee [id=1, firstName=Jamez, lastName=gupta]
Employee [id=2, firstName=BobRobert, lastName=motto]
欢迎来到之路教程(on itroad-com)

示例:序列化对象的ArrayList

下面的程序中,Employee类实现了 Serializable接口。

import java.io.Serializable;
public class Employee implements Serializable {
    String id;
    String firstName;
    String lastName;
    //Getters and setters
    public Employee(String id, String firstName, String lastName) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
    }
}
public class ArrayListExample 
{
    public static void main(String[] args) throws Exception 
    {
        ArrayList<Employee> employees = new ArrayList<>();

        employees.add(new Employee("1", "Jamez", "gupta"));
        employees.add(new Employee("2", "BobRobert", "motto"));
        try 
        {
            FileOutputStream fos = new FileOutputStream("employeeData");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(employees);
            oos.close();
            fos.close();
        } 
        catch (IOException ioe) 
        {
            ioe.printStackTrace();
        }
    }
}

注意, 如果Employee类没有实现Serializable接口,我们会收到这个错误。

java.io.NotSerializableException: com.onitroad.demo.model.Employee
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
	at java.util.ArrayList.writeObject(ArrayList.java:766)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1128)
	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
	at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
	at com.onitroad.demo.ArrayListExample.main(ArrayListExample.java:23)
在 Java 中如何 序列化/反序列化 数组列表

在 Java 中,ArrayList 类默认是可序列化的。
它本质上意味着我们不需要显式实现 Serializable接口来序列化 ArrayList 。

我们可以直接使用 ObjectOutputStream 序列化 ArrayList ,使用 ObjectInputStream 反序列化一个 arraylist 对象。

注意 arraylist 中存储的元素也应该是可序列化的,否则程序将抛出 NotSerializableException

日期:2020-09-17 00:09:26 来源:oir作者:oir