Java 序列化與還原序列化(Stream)

來源:互聯網
上載者:User
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.Properties;public class Properties序列化和還原序列化{/** * @param args */public static void main(String[] args){try{Person p=new Person("曉洋","男",18);//初始化對象//檔案輸出資料流是用於將資料寫入 File 或 FileDescriptor 的輸出資料流。//檔案是否可用或能否可以被建立取決於基礎平台。特別是某些平台一次只允許一個 FileOutputStream//(或其他檔案寫入對象)開啟檔案進行寫入。在這種情況下,如果所涉及的檔案已經開啟,則此類中的構造方法將失敗。 FileOutputStream fos=new FileOutputStream("C:\\1.txt");ObjectOutputStream oos=new ObjectOutputStream(fos);oos.writeObject(p);oos.flush();oos.close();System.out.println("P1:"+p.toString());   }catch (Exception  e){System.out.println(e.toString());}try{Person p2=new Person();FileInputStream fis=new FileInputStream("C:\\1.txt");ObjectInputStream ois=new ObjectInputStream(fis);p2=(Person)ois.readObject();ois.close();System.out.print("P2:"+p2.toString());}catch (Exception e){System.out.println(e.toString());}}}class Person implements Serializable{String name;String sex;Integer age;public String getName(){return name;}public void setName(String name){this.name = name;}public String getSex(){return sex;}public void setSex(String sex){this.sex = sex;}public Integer getAge(){return age;}public void setAge(Integer age){this.age = age;}public Person(){}public Person(String name, String sex, Integer age){super();this.name = name;this.sex = sex;this.age = age;}@Overridepublic String toString(){return "Person [name=" + name + ", sex=" + sex + ", age=" + age + "]";}}

  序列化

     1,要序列化的對象必須繼承Serializable介面 

      2,建立FileOutputStream 對象,指定序列化的檔案

      3,建立ObjectOutputStream對象,指定要序列化對象的FileoutputStream流

      4,通過ObjectOutputStream 流,將p對象寫入到檔案:writeObject(p);

      5,重新整理ObjectOutputStream流

      6,關閉ObjectOutputStream流

 

還原序列化

    1,建立FileInputStream 對象,指定要還原序列化的檔案

    2,建立ObjectInputStream對象,指定要還原序列化對象的FileInputStream流

     3,通過ObjectInputStream 流,readObject();   讀出來,並轉換為Person類型

     4,關閉ObjectInputStream流

 

import java.io.Externalizable;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInput;import java.io.ObjectInputStream;import java.io.ObjectOutput;import java.io.ObjectOutputStream;import java.io.Serializable;public class Externalizable外部序列化{  public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException  {  Student s=new Student();ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("C:\\2.txt"));oos.writeObject(s);//調用Externalizable的writeExternal方法oos.flush();oos.close();System.out.println(s);Student s1=new Student();ObjectInputStream ois =new ObjectInputStream(new FileInputStream("C:\\2.txt"));s1=(Student)ois.readObject();//調用Externalizable的readExternal方法ois.close();System.out.println(s1);  }}class Student implements Externalizable{public Student(){}@Override//外部序列化public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException   {System.out.println( "StudentRead");}@Overridepublic void writeExternal(ObjectOutput out) throws IOException{System.out.println( "StudentWrite");}}//輸出結果//StudentWrite//Student@b66cc//StudentRead//Student@f84386

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.