对象流

对象流:

对象进行序列化(解释一):

        所谓对象序列化就是将对象的状态转换成字节流,以后可以通过这些值(字节序列)再生成相同状态的对象。

对象序列化(解释二):
       
当需要传输对象时,由于对象是很多属性和行为的封装,数据量很庞大。所以在传输对象之前需要将对象打散成字节序列,以利于传输,这个过程

 称为序列化过程。到达目的地以后,又需要将字节序列还原成对象,这个过程称为反序列化过程。

如何实现序列化:
       将需要传输的类,实现java.io.Serializable。这意味着该类的对象可以被打散成字节序列。

       transient 为属性修饰符。表示在传输对象时,该属性的值不做传输。、

代码实现:

  1 package 对象流;
  2 
  3 import java.io.FileInputStream;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.io.ObjectInputStream;
  9 import java.io.ObjectOutputStream;
 10 import java.io.OutputStream;
 11 
 12 public class ObjectTest {
 13     /**
 14      * 将对象写入指定文件
 15      * @param obj
 16      */
 17     public void write(Object obj){
 18         //创建输出流
 19         OutputStream  out=null;
 20         //创建对象输出流
 21         ObjectOutputStream objOut=null;
 22         
 23         try {
 24             
 25             out=new FileOutputStream("obj.txt");
 26             
 27             objOut=new ObjectOutputStream(out);
 28             //写入对象,对象obj必须实现序列化接口
 29             objOut.writeObject(obj);
 30             
 31             
 32         } catch (Exception e) {
 33             // TODO Auto-generated catch block
 34             e.printStackTrace();
 35         }finally{
 36             try {
 37                 objOut.close();
 38                 out.close();
 39             } catch (IOException e) {
 40                 e.printStackTrace();
 41             }
 42         }
 43     }
 44     
 45     public Object read(){
 46         Object obj=null;
 47         
 48         InputStream in=null;
 49         ObjectInputStream objIn=null;
 50         
 51         try {
 52             in=new FileInputStream("obj.txt");
 53             objIn=new ObjectInputStream(in);
 54             //从文件中读取对象数据
 55             obj=objIn.readObject();
 56             
 57         } catch (Exception e) {
 58             // TODO Auto-generated catch block
 59             e.printStackTrace();
 60         }finally{
 61             try {
 62                 objIn.close();
 63                 in.close();
 64             } catch (IOException e) {
 65                 // TODO Auto-generated catch block
 66                 e.printStackTrace();
 67             }
 68         }
 69         
 70         return obj;
 71     }
 72     
 73     public static void main(String[] args) {
 74         ObjectTest test=new ObjectTest();
 75 //        test.write(new Man(1,"姓名","电话",5757));        
 76         System.out.println(test.read());
 77     }
 78 }
 79 
 80 package 对象流;
 81 
 82 import java.io.Serializable;
 83 
 84 /**
 85  * 公民类
 86  * @author Administrator
 87  *
 88  */

//Man类实现了Serializable,表示该类对象可以被序列化,可以传输该对象。 89 public class Man implements Serializable{ 90 /**编号*/ 91 private int id; 92 /**姓名*/ 93 private String name; 94 /**电话*/ 95 private String tel; 96 //该属性加上transient修饰符后,表示该对象在做序列化传输时,money的值 不会被序列化,即不会被传输。这样有利于保护数据的安全性  97 private transient int money; 98 99 public int getId() { 100 return id; 101 } 102 103 public void setId(int id) { 104 this.id = id; 105 } 106 107 public String getName() { 108 return name; 109 } 110 111 public void setName(String name) { 112 this.name = name; 113 } 114 115 public String getTel() { 116 return tel; 117 } 118 119 public void setTel(String tel) { 120 this.tel = tel; 121 } 122 123 public int getMoney() { 124 return money; 125 } 126 127 public void setMoney(int money) { 128 this.money = money; 129 } 130 @Override 131 public String toString() { 132 return "Man [id=" + id + ", name=" + name + ", tel=" + tel + ", money=" 133 + money + "]"; 134 } 135 136 public Man(int id, String name, String tel, int money) { 137 super(); 138 this.id = id; 139 this.name = name; 140 this.tel = tel; 141 this.money = money; 142 } 143 144 public Man() { 145 super(); 146 // TODO Auto-generated constructor stub 147 } 148 }

 

posted on 2017-04-07 23:25  向往的生活  阅读(499)  评论(0编辑  收藏  举报

导航

页面底部