代码示例:
public class testObjectIntOutputStream { //对象的反序列化过程:将硬盘中的文件通过ObjectInputStream转换为相应的对象 @Test public void testObjectInputStream() { ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream("person.txt")); Person p1 = (Person) ois.readObject(); System.out.println(p1); Person p2 = (Person) ois.readObject(); System.out.println(p2); } catch (Exception e) { e.printStackTrace(); } finally { if (ois !=null){ try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } } } //对象的序列化过程,将内存中的对象通过ObjectOutputStream转换为二进制流,存储在硬盘文件中 @Test public void testObjectOutputStream() { Person p1 = new Person("小明", 23,new Pet("花花")); Person p2 = new Person("小红", 21,new Pet("石头")); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("person.txt")); oos.writeObject(p1); oos.flush(); oos.writeObject(p2); oos.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (oos != null) { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } } } /* * 要实现序列化的类:1.要求此类是可序列化的:实现Serializable接口 * 2.要求类的属性同样的要实现Serializable接口 * 3.提供一个版本号:public static final long serialVersionUID * 4.使用static或transient修饰的属性,不可实序列化 * */ class Person implements Serializable { public static final long serialVersionUID = 2332423423442131L; static String name; transient Integer age; Pet pet; public Person(String name, Integer age,Pet pet) { this.name = name; this.age = age; this.pet = pet; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", pet=" + pet + '}'; } } class Pet implements Serializable{ String name; public Pet(String name) { this.name = name; } @Override public String toString() { return "Pet{" + "name='" + name + '\'' + '}'; } }public class testObjectIntOutputStream { //对象的反序列化过程:将硬盘中的文件通过ObjectInputStream转换为相应的对象 @Test public void testObjectInputStream() { ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream("person.txt")); Person p1 = (Person) ois.readObject(); System.out.println(p1); Person p2 = (Person) ois.readObject(); System.out.println(p2); } catch (Exception e) { e.printStackTrace(); } finally { if (ois !=null){ try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } } } //对象的序列化过程,将内存中的对象通过ObjectOutputStream转换为二进制流,存储在硬盘文件中 @Test public void testObjectOutputStream() { Person p1 = new Person("小明", 23,new Pet("花花")); Person p2 = new Person("小红", 21,new Pet("石头")); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("person.txt")); oos.writeObject(p1); oos.flush(); oos.writeObject(p2); oos.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (oos != null) { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } } } /* * 要实现序列化的类:1.要求此类是可序列化的:实现Serializable接口 * 2.要求类的属性同样的要实现Serializable接口 * 3.提供一个版本号:public static final long serialVersionUID * 4.使用static或transient修饰的属性,不可实序列化 * */ class Person implements Serializable { public static final long serialVersionUID = 2332423423442131L; static String name; transient Integer age; Pet pet; public Person(String name, Integer age,Pet pet) { this.name = name; this.age = age; this.pet = pet; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", pet=" + pet + '}'; } } class Pet implements Serializable{ String name; public Pet(String name) { this.name = name; } @Override public String toString() { return "Pet{" + "name='" + name + '\'' + '}'; } }