標籤:class cep puts java序列化 oid file read 等等 輸出
紙上得來終覺淺,絕知此事要躬行 --陸遊 問渠那得清如許,為有源頭活水來 --朱熹
什麼是Java序列化?為什麼出現Java序列化?如何?Java序列化?一、什麼是Java序列化Java序列化是指把Java對象轉換為位元組序列的過程;而Java還原序列化是指把位元組序列恢複為Java對象的過程。
二、為什麼出現Java序列化兩個進程之間進行通訊時,須要傳輸各種資訊。比方文本,映像,聲音等等,這些資訊是通過二進位流的形式進行傳輸的。
那麼進程之間是不是也能夠傳遞對象資料呢?答案是能夠的。Java的序列化和還原序列化就是將Java對象轉化為位元組序列,並在網路上進行傳輸。還原序列化將獲得的位元組序列資料產生對象。
三、如何?Java序列化
1)、對象的輸入輸出:ObjectInputStream,ObjectOutputStream。2)、對象要實現Serializable或者Externalizable介面。否則會拋出異常。
注意:在Serializable中聲明為static和transient類型的成員資料不能被序列化。
實現Serializable介面的序列化和還原序列化示範範例程式:
public class Student implements Serializable{ private String name; private char sex; public Student() { } public Student(String name,char sex) { this.name = name; this.sex = sex; } public void setName(String name) { this.name = name; } public void setSex(char sex) { this.sex = sex; } public String getName() { return this.name; } public char getSex() { return this.sex; }}public class UseStudent{ public static void main(String[] args) { Student st = new Student("Tom",‘M‘); File file = new File("O:\\Java\\com\\jieke\\io\\student.txt"); try { file.createNewFile(); } catch(IOException e) { e.printStackTrace(); } try { //Student對象序列化過程 FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(st); oos.flush(); oos.close(); fos.close(); //Student對象還原序列化過程 FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); Student st1 = (Student) ois.readObject(); System.out.println("name = " + st1.getName()); System.out.println("sex = " + st1.getSex()); ois.close(); fis.close(); } catch(ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
使用Externalizable介面進行序列化。必需要重寫writeExternal(ObjectOutput output)和readExternal(ObjectInput input)方法使用該介面的序列化方法須要我們來實現。因此能夠對static和transient資料進行序列化。
public class Person implements Externalizable { private static final long serialVersionUID = -842029427676826563L; public static String name; private int age; private transient int workDay = 5; private String fClub; public Person() { System.out.println("none-arg constructor"); } public Person(int age, String fClub) { this.age = age; this.fClub = fClub; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getWorkDay() { return workDay; } public void setWorkDay(int workDay) { this.workDay = workDay; } public String getfClub() { return fClub; } public void setfClub(String fClub) { this.fClub = fClub; } private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject();//運行預設的序列化機制 out.writeInt(workDay); System.out.println("進行中序列持久化"); } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); workDay = in.readInt(); System.out.println("讀取持久化對象"); } @Override public void readExternal(ObjectInput arg0) throws IOException, ClassNotFoundException { // TODO Auto-generated method stub } @Override public void writeExternal(ObjectOutput arg0) throws IOException { // TODO Auto-generated method stub }}public class Hello { public static void main(String[] args) { Person person = new Person(26, "Juventus"); person.setWorkDay(7); try { FileOutputStream fs = new FileOutputStream("foo.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(person); os.close(); Person.name = "Alex"; FileInputStream in = new FileInputStream("foo.ser"); ObjectInputStream s = new ObjectInputStream(in); Person p = (Person) s.readObject(); System.out.println("name==" + Person.name + " age==" + p.getAge() + " workDay==" + p.getWorkDay() + " fClub==" + p.getfClub()); } catch (Exception e) { e.printStackTrace(); } }}
當然還有非常多開源的序列化和還原序列化的庫。能夠在實際的project中進行使用。
Java序列化Serializable和Externalizable