objectOutputStream

 

UserVo.java代码

package org.scd;

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

public class UserVo implements Serializable {

     /**
     *
     */
    private static final long serialVersionUID = 6060787593808263896L;
    private String userId;   
      
    private String userName;   
 
    private int age;   
 
    private Date born;  
    public UserVo(){
        super();
    }

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

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

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBorn() {
        return born;
    }

    public void setBorn(Date born) {
        this.born = born;
    }

    
      

}

 

O bjectFileConvert.java代码

package org.scd;

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>();   
        for(int i=0;i<10;i++){
             list.add("michael"+i);   
             list.add("大大" + i);   
            
        }
       
 
        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);   
 
    }   
 

posted @ 2015-07-17 17:36  yangxu6069  阅读(230)  评论(0编辑  收藏  举报