对象序列化 代码

    技术2022-05-20  33

     Student.java

    package ZHANG.IO.Serialization; import java.io.Serializable; public class Student implements Serializable { int id; String name; int age; String department; public Student(int id, String name, int age, String department) { this.id = id; this.name = name; this.age = age; this.department = department; } }

     

    Serialization.java

    package ZHANG.IO.Serialization; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class Serialization { public static void main(String[] args) { Student stu1 = new Student(21,"zhangsan",25,"computer"); Student stu2 = new Student(22,"baiyan",22,"huaxue"); try { FileOutputStream fos = new FileOutputStream("student.txt"); ObjectOutputStream os = new ObjectOutputStream(fos); os.writeObject(stu1); os.writeObject(stu2); os.close(); FileInputStream fis = new FileInputStream("student.txt"); ObjectInputStream ois = new ObjectInputStream(fis); stu1 = (Student) ois.readObject(); stu2 = (Student) ois.readObject(); System.out.println("student1 details:"+stu1.name+" "+stu1.age+" "+stu1.id+" "+stu1.department); System.out.println("student2 details:"+stu2.name+" "+stu2.age+" "+stu2.id+" "+stu2.department); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

    ===========

    private transient String name;   //不需要被序列化 加transient=-=-=

    另一个列子:

    package com.serDemo; import java.io.Serializable; public class Person implements Serializable{ private String name; //不需要被序列化 加transient private int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } @Override public String toString() { return "姓名:"+name+",年龄:"+age; } }

    package com.serDemo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class ObjectoutputStreamDemo { public static void main(String[] args) throws Exception, IOException { File file = new File("d://person.ser"); ObjectOutputStream oos = null; oos = new ObjectOutputStream(new FileOutputStream(file)); Person p = new Person("战士12",34); oos.writeObject(p); oos.close(); } }

     

    package com.serDemo; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectInputStreamDemo { public static void main(String[] args) throws Exception, IOException { File file = new File("d://person.ser"); ObjectInputStream ois = null; ois = new ObjectInputStream(new FileInputStream(file)); Person p =(Person)ois.readObject(); System.out.println(p); } }

    //对一组对象进行序列化

    package com.serDemo; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ArraySerDemo { public static void main(String[] args) throws Exception { Person[] per = {new Person("王武",33),new Person("王六",44),new Person("司机",55)}; ser(per); Person p[] = (Person[])dser(); print(p); } public static void ser(Object obj) throws Exception{ File file = new File("d://person.ser"); ObjectOutputStream oos = null; oos = new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(obj); oos.close(); } public static Object dser() throws Exception{ Object temp = null; File file = new File("d://person.ser"); ObjectInputStream ois = null; ois = new ObjectInputStream(new FileInputStream(file)); temp =ois.readObject(); return temp; } public static void print(Person per[]){ for(Person p : per){ System.out.println(p); } } }


    最新回复(0)