Java对象序列化ObjectOutputStream和ObjectInputStream示例

         blog迁移至 :http://www.micmiu.com

 

Java中ObjectInputStream 与 ObjectOutputStream这两个包装类可用于输入流中读取对象类数据和将对象类型的数据写入到底层输入流 。ObjectInputStream 与 ObjectOutputStream 类所读写的对象必须实现了 Serializable 接口。需要注意的是:对象中的 transient 和 static 类型的成员变量不会被读取和写入

 

具体代码示例:

 

O bjectFileConvert.java

 

package michael.io;

import java.io.File;
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.Date;
import java.util.List;


/**
 * @blog http://sjsky.iteye.com
 * @author Michael
 */
public class ObjectFileConvert {

    /**
     * 文件转化为Object
     * @param fileName
     * @return byte[]
     */
    public static Object file2Object(String fileName) {

        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(fileName);
            ois = new ObjectInputStream(fis);
            Object object = ois.readObject();
            return object;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 把Object输出到文件
     * @param obj
     * @param outputFile
     */
    public static void object2File(Object obj, String outputFile) {
        ObjectOutputStream oos = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(outputFile));
            oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
    }

    /**
     * @param args
     */
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        String fileName = "d:/test/object.obj";
        List<String> list = new ArrayList<String>();
        list.add("michael");
        list.add("大大");

        ObjectFileConvert.object2File(list, fileName);
        System.out.println("success write List<String> to file.");

        List<String> tmpList = (List<String>) ObjectFileConvert
                .file2Object(fileName);
        for (String tmp : tmpList) {
            System.out.println(tmp);
        }

        System.out.println("--------------------------------");

        fileName = "d:/test/uservo.obj";
        UserVo vo = new UserVo("michael", "大大", 18, new Date());

        ObjectFileConvert.object2File(vo, fileName);
        System.out.println("success write bean:UserVo to file.");

        UserVo tmpvo = (UserVo) ObjectFileConvert.file2Object(fileName);
        System.out.println("read bean:UserVo from file get info : " + tmpvo);

    }

}

 

UserVo.java

package michael.io;

import java.io.Serializable;
import java.util.Date;

/**
 * @blog http://sjsky.iteye.com
 * @author Michael
 */
public class UserVo implements Serializable {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -6846034858002233878L;

    private String userId;

    private String userName;

    private int age;

    private Date born;

    public UserVo() {
    }

    public UserVo(String userId, String userName, int age, Date born) {
        this.userId = userId;
        this.userName = userName;
        this.age = age;
        this.born = born;
    }

    /**
     * @return the userId
     */
    public String getUserId() {
        return userId;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }

    /**
     * @return the born
     */
    public Date getBorn() {
        return born;
    }

    /**
     * @param pUserId the userId to set
     */
    public void setUserId(String pUserId) {
        userId = pUserId;
    }

    /**
     * @param pUserName the userName to set
     */
    public void setUserName(String pUserName) {
        userName = pUserName;
    }

    /**
     * @param pAge the age to set
     */
    public void setAge(int pAge) {
        age = pAge;
    }

    /**
     * @param pBorn the born to set
     */
    public void setBorn(Date pBorn) {
        born = pBorn;
    }

    @Override
    public String toString() {
        return "userId=[ " + userId + " ] userName=[ " + userName + " ] age=[ "
                + age + " ] born=[ " + born + "] .";
    }

}
 
运行结果如下:
success write List<String> to file.
michael
大大

--------------------------------
success write bean:UserVo to file.
read bean:UserVo from file get info : userId=[ michael ] userName=[ 大大 ] age=[ 18 ] born=[ Mon Aug 01 13:49:33 CST 2011] .

 

本文连接:http://sjsky.iteye.com/blog/1137231

 

 

转载请注明来自:Michael's blog @ http://sjsky.iteye.com

----------------------------- 分 ------------------------------ 隔 ------------------------------ 线 ------------------------------

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值