Java List序列化的实现


转载请标明出处:
http://blog.csdn.net/tyzlmjj/article/details/50332815
本文出自:【M家杰的博客】

概述
java中的序列化与反序列化都要求对象实现Serializable接口(其实就是声明一下),而对于List这种动态改变的集合默认是不实现这个接口的,也就是不能直接序列化。但是数组是可以序列化的,所以我们只需要将List集合与数组进行转换就可以实现序列化与反序列化了。


序列化

Object对象

public class TestObject implements Serializable{

    private String name;
    private String address;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

}

实例化对象,加点数据,然后执行序列化

public class Test {

    public static void main(String[] args) 
    {
        //创建要序列化的集合对象
        List list = new ArrayList<>();
        //加数据
        for (int i = 0; i < 5; i++) 
        {
            TestObject testObject = new TestObject();
            testObject.setName("MJJ-"+i);
            testObject.setAddress("HangZhou");
            list.add(testObject);
        }

        File file = new File("object.adt");
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file)))
        {
            //将List转换成数组
            TestObject[] obj = new TestObject[list.size()];
            list.toArray(obj);
            //执行序列化存储
            out.writeObject(obj);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

反序列化

public class Test {

    public static void main(String[] args) 
    {
        File file = new File("object.adt");
        try (ObjectInputStream out = new ObjectInputStream(new FileInputStream(file)))
        {
            //执行反序列化读取
            TestObject[] obj = (TestObject[]) out.readObject();
            //将数组转换成List
            List listObject = Arrays.asList(obj);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        } 
        catch (ClassNotFoundException e) 
        {
            e.printStackTrace();
        }
    }
}

封装

利用泛型把序列化和反序列化的方法封装起来,方便使用。

工具类

public class StreamUtils {

    /**
     * 序列化,List
     */
    public static  boolean writeObject(List list,File file)
    {
        T[] array = (T[]) list.toArray();
        try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) 
        {
            out.writeObject(array);
            out.flush();
            return true;
        }
        catch (IOException e) 
        {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 反序列化,List
     */
    public static  List readObjectForList(File file)
    {
        E[] object;
        try(ObjectInputStream out = new ObjectInputStream(new FileInputStream(file))) 
        {
            object = (E[]) out.readObject();
            return Arrays.asList(object);
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        } 
        catch (ClassNotFoundException e) 
        {
            e.printStackTrace();
        }
        return null;
    }
}

使用工具类

//序列化
StreamUtils.writeObject(list, new File("object.adt"));
//反序列化
List re = StreamOfByte.readObjectForList(new File("object.txt"));

你可能感兴趣的:(java基础)