Java對象序列化操作詳解

Java對象序列化操作詳解

長沙一度軟件培訓      2022-04-25 20:35:01     9

Java對象序列化操作詳解, 最近有好多學員問小編Java對象序列化操作的問題,今天長沙一度軟件培訓java學院小編用實例講述Java對象序列化操作,希望看完此

課程價格 請咨詢

上課時段: 授課校區(qū):

詳細介紹

 


最近有好多學員問小編Java對象序列化操作的問題,今天長沙一度軟件培訓java學院小編用實例講述Java對象序列化操作,希望看完此文后,對大家有幫助。



  當兩個進程在進行遠程通信時,彼此可以發(fā)送各種類型的數(shù)據(jù)。無論是何種類型的數(shù)據(jù),都會以二進制序列的形式在網(wǎng)絡(luò)上傳送。發(fā)送方需要把這個Java對象轉(zhuǎn)換為字節(jié)序列,才能在網(wǎng)絡(luò)上傳送;接收方則需要把字節(jié)序列再恢復為Java對象。


  只能將支持 java.io.Serializable 接口的對象寫入流中。每個 serializable 對象的類都被編碼,編碼內(nèi)容包括類名和類簽名、對象的字段值和數(shù)組值,以及從初始對象中引用的其他所有對象的閉包。


  概念


  序列化:把Java對象轉(zhuǎn)換為字節(jié)序列的過程。


  反序列化:把字節(jié)序列恢復為Java對象的過程。


  用途


  對象的序列化主要有兩種用途:


  (1)把對象的字節(jié)序列永久地保存到硬盤上,通常存放在一個文件中;


 ?。?)在網(wǎng)絡(luò)上傳送對象的字節(jié)序列。


  對象序列化


  序列化API


  java.io.ObjectOutputStream代表對象輸出流,它的writeObject(Object obj)方法可對參數(shù)指定的obj對象進行序列化,把得到的字節(jié)序列寫到一個目標輸出流中。只有實現(xiàn)了Serializable和Externalizable接口的類的對象才能被序列化。


  java.io.ObjectInputStream代表對象輸入流,它的readObject()方法從一個源輸入流中讀取字節(jié)序列,再把它們反序列化為一個對象,并將其返回。


  代碼示例


  import java.io.*;

  import java.util.Date;

  public class ObjectSaver {

  public static void main(String[] args) throws Exception {

  

  //序列化對象

  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;

  }

  }



  執(zhí)行結(jié)果

  


  說明


  讀取對象的順序與寫入時的順序要一致。


  對象的默認序列化機制寫入的內(nèi)容是:對象的類,類簽名,以及非瞬態(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判斷鍵盤輸入,程序可能會無法結(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 +

  '}';

  }

  }


  實體需要實現(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)鍵字,實體的屬性使用該關(guān)鍵子,進行序列化時該屬性值將不會被保存,反序列化的結(jié)果為,該屬性的值為該屬性類型的默認值。


  private String title;

  private transient Integer number;



以上就是長沙一度軟件培訓java學院小編介紹的“Java對象序列化操作詳解”的內(nèi)容,希望對大家有幫助,更多精彩內(nèi)容請關(guān)注長沙一度軟件培訓java學院官網(wǎng),每天會有java最新內(nèi)容分享與你。



培訓啦提醒您:交易時請核實對方資質(zhì),對于過大宣傳或承諾需謹慎!任何要求預付定金、匯款等方式均存在風險,謹防上當。