package com.io.homework;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.junit.Test;/** *序列化和反序列化 */public class SerializeDemo{ /* * 序列化 : 创建三个不同的Person对象,将这三个对象序列化到文件中; */@Testpublic void serializeTest() { Person person1 = new Person("李明", 24, 60.2); Person person2 = new Person("jack", 22, 67.5); Person person3 = new Person("斯密瑟", 26, 70.2); Listlist = new ArrayList<>(); list.add(person1); list.add(person2); list.add(person3);// 3、创建ObjectOutputStream 的实例完成序列化ObjectOutputStream oos = null;try {// 1、创建输出流 FileOutputStreamFileOutputStream fos = new FileOutputStream("./serialize.dat");// 2、创建缓冲包装节点流BufferedOutputStream bos = new BufferedOutputStream(fos); oos = new ObjectOutputStream(bos);// 4、通过标识对应的数据类型,将数据写入目标地点 oos.writeObject(list); } catch (IOException e) { e.printStackTrace(); } finally {// 5、关闭流if (oos != null) {try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println("写入成功!!"); }/* * 反序列化:三个Person对象,打印输出。验证序列化的正确; */@Testpublic void deserializationTest() {// 2. 创建 ObjectInputStream 的实例,包装现有节点流,用于完成反序列化ObjectInputStream ois = null;try {// 1. 创建 FileInputStream 的实例FileInputStream fis = new FileInputStream("./serialize.dat"); BufferedInputStream bis = new BufferedInputStream(fis); ois = new ObjectInputStream(bis); @SuppressWarnings("unchecked") Listlist = (List) ois.readObject(); Iteratorit = list.iterator();// 3. 读取文件的内容while (it.hasNext()) { Person person = it.next(); System.out.println(person); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally {if (ois != null) {try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } } } }//编写Person类,包含姓名、年龄、体重等属性,提供对应的访问方法;class Person implements Serializable {private static final long serialVersionUID = 314398401529574184L;private String name;// 姓名private Integer age;// 年龄private double whight;// 体重/** * @param name * @param age * @param whight */public Person(String name, Integer age, double whight) {super();this.name = name;this.age = age;this.whight = whight; }public Person() {super(); }/** * @return the name */public String getName() {return name; }/** * @param name the name to set */public void setName(String name) {this.name = name; }/** * @return the age */public Integer getAge() {return age; }/** * @param age the age to set */public void setAge(Integer age) {this.age = age; }/** * @return the whight */public double getWhight() {return whight; }/** * @param whight the whight to set */public void setWhight(double whight) {this.whight = whight; } @Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + ", whight=" + whight + "]"; } }
Copyright © 2004-2024 Ynicp.com 版权所有 法律顾问:建纬(昆明)律师事务所 昆明市网翼通科技有限公司 滇ICP备08002592号-4