Java序列化把一個物件Java Object
變成一個二進位位元組序列byte[]
Java反序列化就是把一個二進位位元組序列byte[]
變成Java物件Java Object
#序列化API
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("1.txt")); 或 FileOutputStream fout = new FileOutputStream("1.txt"); ObjectOutputStream out = new ObjectOutputStream(fout);此外ObjectOutputStream也提供了
writeObject()
方法來序列化一個對象,並將它傳送到輸出流。ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("1.txt")); out.writeObject(new Test("Sentiment",10)); //对Test类中的构造方法进行传参ObjectInputStream
位元組輸入流對象,將檔案中的二進位位元組序列進行反序列化操作(結合FileInputStream)
實例:
ObjectInputStream in = new ObjectInputStream(new FileInputStream("1.txt")); 或 FileInputStream fin = new FileInputStream("1.txt"); ObjectInputStream oin = new ObjectInputStream(fin);
此外ObjectInputStream也提供readObject()
方法從流中取出下一個對象,並將物件反序列化。它的傳回值為Object,因此,需要將它轉換成合適的資料類型。ObjectInputStream In = new ObjectInputStream(new FileInputStream("1.txt")); Test o = (Test)In.readObject(); System.out.println(o); //读取后输出实例理解
package Sentiment.unserialize.demo01; import java.io.Serializable; public class Test implements Serializable { private String name; private int age; public Test() { } public Test(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Test{" + "name='" + name + '\'' + ", age=" + age + '}'; } }Serializable.java
package Sentiment.unserialize.demo01; import java.io.*; public class Serializable { public static void main(String[] args) throws IOException { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("1.txt")); out.writeObject(new Test("Sentiment",10)); out.close(); } }
package Sentiment.unserialize.demo01; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class UnSerializable { public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream In = new ObjectInputStream(new FileInputStream("1.txt")); Test o = (Test)In.readObject(); System.out.println(o); } }執行Serializable.java後得到二進位字節碼檔內容
serialVersionUID
在Test.java定義serialVersionUID
其值為1 <pre class="brush:java;">package Sentiment.unserialize.demo01;
import java.io.Serializable;
public class Test implements Serializable {
public static final long serialVersionUID = 1L; //定义serialVersionUID
private String name;
private int age;
public Test() {
}
public Test(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Test{" + "name=&#39;" + name + &#39;\&#39;&#39; + ", age=" + age + &#39;}&#39;;
}
}</pre>
此時執行Serializable.java進行序列化操作 完成完成後將
serialVersionUID
的值定義為2—>public static final long serialVersionUID = 2L;,在運作UnSerializable.java會報錯誤
Transient (瞬態變數)
實例理解
和
age變數前分別加上transient
和## #static######package Sentiment.unserialize.demo01; import java.io.Serializable; public class Test implements Serializable { public static final long serialVersionUID = 1L; //定义serialVersionUID private transient String name; //加上transient private static int age; //加上static public Test() { } public Test(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Test{" + "name='" + name + '\'' + ", age=" + age + '}'; } }###此時在執行Serializable.java和UnSerializable.java,輸出結果:Sentiment—>null,10—>0############ ###readObject()方法重寫######官方允許使用者在被序列化的類別中重寫readObject() 方法, 重寫後的方法將負責在反序列化時重構目前類別物件.使用者只需要在重寫的readObject() 方法中實作defaultReadObject() 方法, 就可以確保反序列化過程正常執行.######實例理解######實作defaultReadObject() 方法呼叫,並在Test類別中重寫該方法,在進行序列化和反序列化操作即可執行命令###
private void readObject(java.io.ObjectInputStream a) throws IOException, ClassNotFoundException{ a.defaultReadObject(); Runtime.getRuntime().exec("calc.exe"); }#########
以上是Java序列化與反序列化怎麼應用的詳細內容。更多資訊請關注PHP中文網其他相關文章!