Object stream, java object stream
Object stream:
Object serialization (Interpretation 1 ):
Object serialization refers to converting the object state into word throttling. You can use these values (byte sequences)Generate objects in the same status.
Object serialization (Interpretation 2 ):
When an object needs to be transmitted, the data volume is huge because the object is encapsulated by many attributes and behaviors. Therefore, before transmitting an object, we need to split the object into a byte sequence to facilitate transmission.
It is called a serialization process. After arriving at the destination, you need to restore the byte sequence to an object. This process is called the deserialization process.
How to Implement serialization:
Implement java. io. Serializable for classes to be transmitted. This means that the objects of this class can be split into byte sequences.
Transient is the attribute modifier. Indicates that the value of this attribute is not transmitted during object transmission. ,
Code implementation:
1 package object stream; 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 * write the Object to the specified file 15 * @ param obj 16 */17 public void write (Object obj) {18 // create output stream 19 OutputStream out = null; 20 // create object output stream 21 ObjectOutputStream objOut = null; 22 23 try {24 25 out = new FileOutputStream ("obj.txt"); 26 27 objOut = new ObjectOutputStream (out); 28 // write object, object obj must implement the serialization interface 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 // read object data from the file 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, "name", "phone", 5757); 76 System. out. println (test. read (); 77} 78} 79 80 package object stream; 81 82 import java. io. serializable; 83 84/** 85 * citizen class 86 * @ author Administrator 87*88 */
// The Man class implements Serializable, indicating that the class object can be serialized and can be transmitted. 89 public class Man implements Serializable {90/** no. */91 private int id; 92/** name */93 private String name; 94/** phone */95 private String tel; 96 // After the transient modifier is added to this property, the value of money will not be serialized when the object is being serialized and transmitted, that is, it will not be transmitted. This helps protect data security 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 @ Override131 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 stub147} 148}