更新時間:2019-08-16 11:34:46 來源:動力節點 瀏覽2281次
最近有好多學員問小編Java對象序列化操作的問題,今天動力節點java學院小編用實例講述Java對象序列化操作,希望看完此文后,對大家有幫助。
當兩個進程在進行遠程通信時,彼此可以發送各種類型的數據。無論是何種類型的數據,都會以二進制序列的形式在網絡上傳送。發送方需要把這個Java對象轉換為字節序列,才能在網絡上傳送;接收方則需要把字節序列再恢復為Java對象。
只能將支持 java.io.Serializable 接口的對象寫入流中。每個 serializable 對象的類都被編碼,編碼內容包括類名和類簽名、對象的字段值和數組值,以及從初始對象中引用的其他所有對象的閉包。
概念
序列化:把Java對象轉換為字節序列的過程。
反序列化:把字節序列恢復為Java對象的過程。
用途
對象的序列化主要有兩種用途:
(1)把對象的字節序列永久地保存到硬盤上,通常存放在一個文件中;
(2)在網絡上傳送對象的字節序列。
對象序列化
序列化API
java.io.ObjectOutputStream代表對象輸出流,它的writeObject(Object obj)方法可對參數指定的obj對象進行序列化,把得到的字節序列寫到一個目標輸出流中。只有實現了Serializable和Externalizable接口的類的對象才能被序列化。
java.io.ObjectInputStream代表對象輸入流,它的readObject()方法從一個源輸入流中讀取字節序列,再把它們反序列化為一個對象,并將其返回。
代碼示例
import java.io.*;
import java.util.Date;
public class ObjectSaver {
public static void main(String[] args) throws Exception {
/*其中的 D:\\objectFile.obj 表示存放序列化對象的文件*/
//序列化對象
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:\\objectFile.obj"));
Customer customer = new Customer("王麻子", 24);
out.writeObject("你好!"); //寫入字面值常量
out.writeObject(new Date()); //寫入匿名Date對象
out.writeObject(customer); //寫入customer對象
out.close();
//反序列化對象
ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:\\objectFile.obj"));
System.out.println("obj1 " + (String) in.readObject()); //讀取字面值常量
System.out.println("obj2 " + (Date) in.readObject()); //讀取匿名Date對象
Customer obj3 = (Customer) in.readObject(); //讀取customer對象
System.out.println("obj3 " + obj3);
in.close();
}
}
class Customer implements Serializable {
private String name;
private int age;
public Customer(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "name=" + name + ", age=" + age;
}
}
執行結果
說明
讀取對象的順序與寫入時的順序要一致。
對象的默認序列化機制寫入的內容是:對象的類,類簽名,以及非瞬態和非靜態字段的值。
常見序列化操作
打印流
public class Hello {
public static void main(String[] args) throws Exception {
File file = new File("E:" + File.separator + "myFile" + File.separator + "test" + File.separator + "123.txt");
OutputStream outputStream = new FileOutputStream(file);
PrintStream printStream = new PrintStream(outputStream);
printStream.print(123);
printStream.println("hello");
printStream.println(12.5);
printStream.close();
}
}
鍵盤輸入讀取到程序中
public class Hello {
public static void main(String[] args) throws Exception {
InputStream in = System.in;
byte[] data = new byte[100];
System.out.println("輸入數據:");
int read = in.read(data);
System.out.println(read);
System.out.println(new String(data,0,read));
}
}
掃碼流
public class Hello {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(new FileInputStream(new File("E:" + File.separator + "myFile" + File.separator + "test" + File.separator + "123.txt")));
scanner.useDelimiter("\n");
while (scanner.hasNext()){
String next = scanner.next();
System.out.println(next);
}
scanner.close();
}
}
scanner.useDelimiter("\n");表示以回車(換行)為定界符,回車間為一段掃碼的內容。
掃描鍵盤輸入
Scanner scanner = new Scanner(System.in);
注意:使用while判斷鍵盤輸入,程序可能會無法結束
對象序列化
序列化操作類:ObjectOutputStream,寫到文件中
public class Hello {
public static void main(String[] args) throws Exception {
A a = new A("hello", 123);
File file = new File("E:" + File.separator + "myFile" + File.separator + "test" + File.separator + "a.ser");
OutputStream outputStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(a);
objectOutputStream.close();
}
}
class A implements Serializable {
private String title;
private Integer number;
public A(String title, Integer number) {
this.title = title;
this.number = number;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
@Override
public String toString() {
return "A{" +
"title='" + title + '\'' +
", number=" + number +
'}';
}
}
實體需要實現可序列化的接口implements Serializable,表示一種能力
反序列化操作類:ObjectInputStream,讀到程序里
public class Hello {
public static void main(String[] args) throws Exception {
File file = new File("E:" + File.separator + "myFile" + File.separator + "test" + File.separator + "a.ser");
InputStream inputStream = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
A a = (A) objectInputStream.readObject();
System.out.println(a);
}
}
transient關鍵字,實體的屬性使用該關鍵子,進行序列化時該屬性值將不會被保存,反序列化的結果為,該屬性的值為該屬性類型的默認值。
private String title;
private transient Integer number;
以上就是動力節點java學院小編介紹的“Java對象序列化操作詳解”的內容,希望對大家有幫助,更多精彩內容請關注動力節點java學院官網,每天會有java最新內容分享與你。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習