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);
writeObject()
方法來序列化一個對象,並將它傳送到輸出流。ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("1.txt")); out.writeObject(new Test("Sentiment",10)); //对Test类中的构造方法进行传参
位元組輸入流對象,將檔案中的二進位位元組序列進行反序列化操作(結合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 + '}'; } }
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); } }
serialVersionUID
在Test.java定義serialVersionUID
其值為1 <div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">登入後複製</div></div>
此時執行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 + '}'; } }
private void readObject(java.io.ObjectInputStream a) throws IOException, ClassNotFoundException{ a.defaultReadObject(); Runtime.getRuntime().exec("calc.exe"); }
以上是Java序列化與反序列化怎麼應用的詳細內容。更多資訊請關注PHP中文網其他相關文章!