读书人

Java对象序列化ObjectOutputStream跟O

发布时间: 2012-11-03 10:57:43 作者: rapoo

Java对象序列化ObjectOutputStream和ObjectInputStream示例

[转]http://sjsky.iteye.com/blog/1137231

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] .

读书人网 >编程

热点推荐