Java学习资料-Java对象序列化ObjectOut(In)putStream

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

具体代码示例:

O bjectFileConvert.java

  1. package michael.io;   

  2.   

  3. import java.io.File;   

  4. import java.io.FileInputStream;   

  5. import java.io.FileOutputStream;   

  6. import java.io.IOException;   

  7. import java.io.ObjectInputStream;   

  8. import java.io.ObjectOutputStream;   

  9. import java.util.ArrayList;   

  10. import java.util.Date;   

  11. import java.util.List;   

  12.   

  13.   

  14. /**  

  15.  * @blog http://sjsky.iteye.com  

  16.  * @author Michael  

  17.  */  

  18. public class ObjectFileConvert {   

  19.   

  20.     /**  

  21.      * 文件转化为Object  

  22.      * @param fileName  

  23.      * @return byte[]  

  24.      */  

  25.     public static Object file2Object(String fileName) {   

  26.   

  27.         FileInputStream fis = null;   

  28.         ObjectInputStream ois = null;   

  29.         try {   

  30.             fis = new FileInputStream(fileName);   

  31.             ois = new ObjectInputStream(fis);   

  32.             Object object = ois.readObject();   

  33.             return object;   

  34.         } catch (Exception e) {   

  35.             e.printStackTrace();   

  36.         } finally {   

  37.             if (fis != null) {   

  38.                 try {   

  39.                     fis.close();   

  40.                 } catch (IOException e1) {   

  41.                     e1.printStackTrace();   

  42.                 }   

  43.             }   

  44.             if (ois != null) {   

  45.                 try {   

  46.                     ois.close();   

  47.                 } catch (IOException e2) {   

  48.                     e2.printStackTrace();   

  49.                 }   

  50.             }   

  51.         }   

  52.         return null;   

  53.     }   

  54.   

  55.     /**  

  56.      * 把Object输出到文件  

  57.      * @param obj  

  58.      * @param outputFile  

  59.      */  

  60.     public static void object2File(Object obj, String outputFile) {   

  61.         ObjectOutputStream oos = null;   

  62.         FileOutputStream fos = null;   

  63.         try {   

  64.             fos = new FileOutputStream(new File(outputFile));   

  65.             oos = new ObjectOutputStream(fos);   

  66.             oos.writeObject(obj);   

  67.         } catch (Exception e) {   

  68.             e.printStackTrace();   

  69.         } finally {   

  70.             if (oos != null) {   

  71.                 try {   

  72.                     oos.close();   

  73.                 } catch (IOException e1) {   

  74.                     e1.printStackTrace();   

  75.                 }   

  76.             }   

  77.             if (fos != null) {   

  78.                 try {   

  79.                     fos.close();   

  80.                 } catch (IOException e2) {   

  81.                     e2.printStackTrace();   

  82.                 }   

  83.             }   

  84.         }   

  85.     }   

  86.   

  87.     /**  

  88.      * @param args  

  89.      */  

  90.     @SuppressWarnings("unchecked")   

  91.     public static void main(String[] args) {   

  92.         String fileName = "d:/test/object.obj";   

  93.         List<String> list = new ArrayList<String>();   

  94.         list.add("michael");   

  95.         list.add("大大");   

  96.   

  97.         ObjectFileConvert.object2File(list, fileName);   

  98.         System.out.println("success write List<String> to file.");   

  99.   

  100.         List<String> tmpList = (List<String>) ObjectFileConvert   

  101.                 .file2Object(fileName);   

  102.         for (String tmp : tmpList) {   

  103.             System.out.println(tmp);   

  104.         }   

  105.   

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

  107.   

  108.         fileName = "d:/test/uservo.obj";   

  109.         UserVo vo = new UserVo("michael""大大"18new Date());   

  110.   

  111.         ObjectFileConvert.object2File(vo, fileName);   

  112.         System.out.println("success write bean:UserVo to file.");   

  113.   

  114.         UserVo tmpvo = (UserVo) ObjectFileConvert.file2Object(fileName);   

  115.         System.out.println("read bean:UserVo from file get info : " + tmpvo);   

  116.   

  117.     }   

  118.   

  119. }  

UserVo.java

  1. package michael.io;   

  2.   

  3. import java.io.Serializable;   

  4. import java.util.Date;   

  5.   

  6. /**  

  7.  * @blog http://sjsky.iteye.com  

  8.  * @author Michael  

  9.  */  

  10. public class UserVo implements Serializable {   

  11.   

  12.     /**  

  13.      * serialVersionUID  

  14.      */  

  15.     private static final long serialVersionUID = -6846034858002233878L;   

  16.   

  17.     private String userId;   

  18.   

  19.     private String userName;   

  20.   

  21.     private int age;   

  22.   

  23.     private Date born;   

  24.   

  25.     public UserVo() {   

  26.     }   

  27.   

  28.     public UserVo(String userId, String userName, int age, Date born) {   

  29.         this.userId = userId;   

  30.         this.userName = userName;   

  31.         this.age = age;   

  32.         this.born = born;   

  33.     }   

  34.   

  35.     /**  

  36.      * @return the userId  

  37.      */  

  38.     public String getUserId() {   

  39.         return userId;   

  40.     }   

  41.   

  42.     /**  

  43.      * @return the userName  

  44.      */  

  45.     public String getUserName() {   

  46.         return userName;   

  47.     }   

  48.   

  49.     /**  

  50.      * @return the age  

  51.      */  

  52.     public int getAge() {   

  53.         return age;   

  54.     }   

  55.   

  56.     /**  

  57.      * @return the born  

  58.      */  

  59.     public Date getBorn() {   

  60.         return born;   

  61.     }   

  62.   

  63.     /**  

  64.      * @param pUserId the userId to set  

  65.      */  

  66.     public void setUserId(String pUserId) {   

  67.         userId = pUserId;   

  68.     }   

  69.   

  70.     /**  

  71.      * @param pUserName the userName to set  

  72.      */  

  73.     public void setUserName(String pUserName) {   

  74.         userName = pUserName;   

  75.     }   

  76.   

  77.     /**  

  78.      * @param pAge the age to set  

  79.      */  

  80.     public void setAge(int pAge) {   

  81.         age = pAge;   

  82.     }   

  83.   

  84.     /**  

  85.      * @param pBorn the born to set  

  86.      */  

  87.     public void setBorn(Date pBorn) {   

  88.         born = pBorn;   

  89.     }   

  90.   

  91.     @Override  

  92.     public String toString() {   

  93.         return "userId=[ " + userId + " ] userName=[ " + userName + " ] age=[ "  

  94.                 + age + " ] born=[ " + born + "] .";   

  95.     }   

  96.   

  97. }  

运行结果如下:

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



转载于:https://my.oschina.net/ysh3940/blog/383149

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值