您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)如何在Java中實(shí)現(xiàn)對(duì)象序列化操作,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
序列化:把Java對(duì)象轉(zhuǎn)換為字節(jié)序列的過程。
反序列化:把字節(jié)序列恢復(fù)為Java對(duì)象的過程。
對(duì)象的序列化主要有兩種用途:
1) 把對(duì)象的字節(jié)序列永久地保存到硬盤上,通常存放在一個(gè)文件中;
2) 在網(wǎng)絡(luò)上傳送對(duì)象的字節(jié)序列。
java.io.ObjectOutputStream
代表對(duì)象輸出流,它的writeObject(Object obj)
方法可對(duì)參數(shù)指定的obj對(duì)象進(jìn)行序列化,把得到的字節(jié)序列寫到一個(gè)目標(biāo)輸出流中。只有實(shí)現(xiàn)了Serializable和Externalizable接口的類的對(duì)象才能被序列化。
java.io.ObjectInputStream
代表對(duì)象輸入流,它的readObject()
方法從一個(gè)源輸入流中讀取字節(jié)序列,再把它們反序列化為一個(gè)對(duì)象,并將其返回。
import java.io.*; import java.util.Date; public class ObjectSaver { public static void main(String[] args) throws Exception { /*其中的 D:\\objectFile.obj 表示存放序列化對(duì)象的文件*/ //序列化對(duì)象 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:\\objectFile.obj")); Customer customer = new Customer("王麻子", 24); out.writeObject("你好!"); //寫入字面值常量 out.writeObject(new Date()); //寫入匿名Date對(duì)象 out.writeObject(customer); //寫入customer對(duì)象 out.close(); //反序列化對(duì)象 ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:\\objectFile.obj")); System.out.println("obj1 " + (String) in.readObject()); //讀取字面值常量 System.out.println("obj2 " + (Date) in.readObject()); //讀取匿名Date對(duì)象 Customer obj3 = (Customer) in.readObject(); //讀取customer對(duì)象 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; } }
執(zhí)行結(jié)果
讀取對(duì)象的順序與寫入時(shí)的順序要一致。
對(duì)象的默認(rèn)序列化機(jī)制寫入的內(nèi)容是:對(duì)象的類,類簽名,以及非瞬態(tài)和非靜態(tài)字段的值。
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("輸入數(shù)據(jù):"); 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");
表示以回車(換行)為定界符,回車間為一段掃碼的內(nèi)容。
掃描鍵盤輸入
Scanner scanner = new Scanner(System.in);
注意:使用while判斷鍵盤輸入,程序可能會(huì)無法結(jié)束
序列化操作類: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 + '}'; } }
實(shí)體需要實(shí)現(xiàn)可序列化的接口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
關(guān)鍵字,實(shí)體的屬性使用該關(guān)鍵子,進(jìn)行序列化時(shí)該屬性值將不會(huì)被保存,反序列化的結(jié)果為,該屬性的值為該屬性類型的默認(rèn)值。
private String title; private transient Integer number;
以上就是如何在Java中實(shí)現(xiàn)對(duì)象序列化操作,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。