HTML學習筆記---參考了網上諸多教程
阿新 • • 發佈:2022-04-13
Java的序列化和反序列化
package com.IO; //自定義類:序列化 import java.io.*; //Serializable必須呼叫他的String中的介面;否者序列號異常==重點 public class Demo02 implements Serializable { //serialVersionUID為java序列號;用於反序列話識別==重點 public static final long serialVersionUID = 12L; private String name; private double hight; public Demo02(String name, double hight) { this.name = name; this.hight = hight; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getHight() { return hight; } public void setHight(double hight) { this.hight = hight; } @Override public String toString() { return "Demo02{" + "name='" + name + '\'' + ", hight=" + hight + '}'; } public Demo02() { } } class Demo03{ public static void main(String[] args) throws IOException { Demo02 demo02 = new Demo02("小茹",160.5); File file =new File("D://IDEA/Demo.txt"); FileOutputStream fileOutputStream =new FileOutputStream(file); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(demo02); objectOutputStream.close(); } }
進行反序列化
package com.IO; import java.io.*; public class Test011 { public static void main(String[] args) throws IOException, ClassNotFoundException { File file = new File("D://IDEA/Demo.txt"); FileInputStream fileInputStream =new FileInputStream(file); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Object o = objectInputStream.readObject(); System.out.println(o); } }