• Java 实现序列化和反序列化


      1 package com.io.homework;
      2 
      3 import java.io.BufferedInputStream;
      4 import java.io.BufferedOutputStream;
      5 import java.io.FileInputStream;
      6 import java.io.FileNotFoundException;
      7 import java.io.FileOutputStream;
      8 import java.io.IOException;
      9 import java.io.ObjectInputStream;
     10 import java.io.ObjectOutputStream;
     11 import java.io.Serializable;
     12 import java.util.ArrayList;
     13 import java.util.Iterator;
     14 import java.util.List;
     15 
     16 import org.junit.Test;
     17 
     18 /**
     19  *序列化和反序列化
     20  */
     21 public class SerializeDemo{
     22      
     23     /*
     24      * 序列化 : 创建三个不同的Person对象,将这三个对象序列化到文件中;
     25      */
     26     @Test
     27     public void serializeTest() {
     28         Person person1 = new Person("李明", 24, 60.2);
     29         Person person2 = new Person("jack", 22, 67.5);
     30         Person person3 = new Person("斯密瑟", 26, 70.2);
     31         List<Person> list = new ArrayList<>();
     32         list.add(person1);
     33         list.add(person2);
     34         list.add(person3);
     35 
     36         // 3、创建ObjectOutputStream 的实例完成序列化
     37         ObjectOutputStream oos = null;
     38         try {
     39             // 1、创建输出流 FileOutputStream
     40             FileOutputStream fos = new FileOutputStream("./serialize.dat");
     41             // 2、创建缓冲包装节点流
     42             BufferedOutputStream bos = new BufferedOutputStream(fos);
     43             oos = new ObjectOutputStream(bos);
     44             // 4、通过标识对应的数据类型,将数据写入目标地点
     45             oos.writeObject(list);
     46         } catch (IOException e) {
     47             e.printStackTrace();
     48         } finally {
     49             // 5、关闭流
     50             if (oos != null) {
     51                 try {
     52                     oos.close();
     53                 } catch (IOException e) {
     54                     e.printStackTrace();
     55                 }
     56             }
     57         }
     58         System.out.println("写入成功!!");
     59 
     60     }
     61 
     62     /*
     63      * 反序列化:三个Person对象,打印输出。验证序列化的正确;
     64      */
     65     @Test
     66     public void deserializationTest() {
     67         // 2. 创建 ObjectInputStream 的实例,包装现有节点流,用于完成反序列化
     68 
     69         ObjectInputStream ois = null;
     70         try {
     71             // 1. 创建 FileInputStream 的实例
     72             FileInputStream fis = new FileInputStream("./serialize.dat");
     73             BufferedInputStream bis = new BufferedInputStream(fis);
     74             ois = new ObjectInputStream(bis);
     75 
     76             @SuppressWarnings("unchecked")
     77             List<Person> list = (List<Person>) ois.readObject();
     78             Iterator<Person> it = list.iterator();
     79 
     80             // 3. 读取文件的内容
     81             while (it.hasNext()) {
     82                 Person person = it.next();
     83                 System.out.println(person);
     84             }
     85         } catch (FileNotFoundException e) {
     86             e.printStackTrace();
     87         } catch (ClassNotFoundException e) {
     88             e.printStackTrace();
     89         } catch (IOException e) {
     90             e.printStackTrace();
     91         } finally {
     92             if (ois != null) {
     93                 try {
     94                     ois.close();
     95                 } catch (IOException e) {
     96                     e.printStackTrace();
     97                 }
     98             }
     99         }
    100 
    101     }
    102 }
    103 
    104 //编写Person类,包含姓名、年龄、体重等属性,提供对应的访问方法;
    105 class Person implements Serializable {
    106 
    107     private static final long serialVersionUID = 314398401529574184L;
    108     private String name;// 姓名
    109     private Integer age;// 年龄
    110     private double whight;// 体重
    111 
    112     /**
    113      * @param name
    114      * @param age
    115      * @param whight
    116      */
    117     public Person(String name, Integer age, double whight) {
    118         super();
    119         this.name = name;
    120         this.age = age;
    121         this.whight = whight;
    122     }
    123 
    124     public Person() {
    125         super();
    126     }
    127 
    128     /**
    129      * @return the name
    130      */
    131     public String getName() {
    132         return name;
    133     }
    134 
    135     /**
    136      * @param name the name to set
    137      */
    138     public void setName(String name) {
    139         this.name = name;
    140     }
    141 
    142     /**
    143      * @return the age
    144      */
    145     public Integer getAge() {
    146         return age;
    147     }
    148 
    149     /**
    150      * @param age the age to set
    151      */
    152     public void setAge(Integer age) {
    153         this.age = age;
    154     }
    155 
    156     /**
    157      * @return the whight
    158      */
    159     public double getWhight() {
    160         return whight;
    161     }
    162 
    163     /**
    164      * @param whight the whight to set
    165      */
    166     public void setWhight(double whight) {
    167         this.whight = whight;
    168     }
    169 
    170     @Override
    171     public String toString() {
    172         return "Person [name=" + name + ", age=" + age + ", whight=" + whight + "]";
    173     }
    174 
    175 }
  • 相关阅读:
    $动态规划系列(1)——金矿模型的理解
    $Java HttpClient库的使用
    $Java-json系列(二):用JSONObject解析和处理json数据
    $百度应用引擎BAE的使用与应用部署
    利用ajax短轮询+php与服务器交互制作简易即时聊天网站
    MYSQL explain详解
    Redis 5种数据结构使用及注意事项
    Redis 存储机制
    memcache
    mysql分表和表分区详解
  • 原文地址:https://www.cnblogs.com/yonxin/p/12508200.html
Copyright © 2020-2023  润新知