java:序列化一个l对象

来源:互联网 发布:js点击div触发事件 编辑:程序博客网 时间:2024/05/05 21:18

package com.csdn.serialize;

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import java.util.List;

 

public class TestAnotherSerialize {

 

    /**

      * @param args

      * @throws IOException

      * @throws ClassNotFoundException

      */

    public static void main(String[] args) throws IOException,

            ClassNotFoundException {

 

        // 1. 准备数据

        List dataList = new ArrayList();

        dataList.add("abc");

        dataList.add("123");

 

        // 2. 构造序列化流

        String sFilePath = "D://test_another_serialize.dat";

        FileOutputStream fos = null;

        ObjectOutputStream ooS = null;

        try {

            fos = new FileOutputStream(sFilePath);

            ooS = new ObjectOutputStream(fos);

            ooS.writeObject(dataList);

        } catch (Exception ex) {

            ex.printStackTrace();

        } finally {

            if (fos != null)

                fos.close();

            if (ooS != null) {

                ooS.close();

            }

        }

 

        // 3. 从序列化文件中读取出文件

        FileInputStream fiStream = null;

        ObjectInputStream oiStream = null;

        try {

            fiStream = new FileInputStream(sFilePath);

            oiStream = new ObjectInputStream(fiStream);

 

            List serializeResult = (List) oiStream.readObject();

            System.out.println(" 序列化结果为::" + serializeResult);

 

        } catch (Exception ex) {

            ex.printStackTrace();

        } finally {

            if (fiStream != null)

                fiStream.close();

            if (oiStream != null) {

                oiStream.close();

            }

        }

    }

}

0 0
原创粉丝点击