• 序列化集合


    package com.itcast.demo06.ObjectStream;

    import java.io.*;
    import java.util.ArrayList;

    /**
    * @author newcityman
    * @date 2019/7/28 - 23:15
    * 练习:序列化集合
    * 步骤:
    * 1、定义一个存储Person对象的ArrayList集合
    * 2、往集合中存储person对象
    * 3、创建一个序列化流ObjectOutputStream,对集合进行序列化
    * 4、使用ObjectOutputStream对象中的方法writeObject,对集合进行序列化
    * 5、创建一个反序列化流ObjectInputStream
    * 6、使用ObjectInputStream对象中的方法readObject读取文件中保存的集合
    * 7、把Object类型的集合转换成ArrayList类型
    * 8、遍历ArrayList结合
    * 9、释放资源
    */
    public class Demo03Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
    // 1、定义一个存储Person对象的ArrayList集合
    ArrayList<Person> pList = new ArrayList<>();
    // 2、往集合中存储person对象
    pList.add(new Person("张山峰",70));
    pList.add(new Person("赵义勇",20));
    pList.add(new Person("李敏",30));
    // 3、创建一个序列化流ObjectOutputStream,对集合进行序列化
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day18_IOAndProperties\person.txt"));
    // 4、使用ObjectOutputStream对象中的方法writeObject,对集合进行序列化
    oos.writeObject(pList);
    // 5、创建一个反序列化流ObjectInputStream
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day18_IOAndProperties\person.txt"));
    // 6、使用ObjectInputStream对象中的方法readObject读取文件中保存的集合
    Object o = ois.readObject();
    // 7、把Object类型的集合转换成ArrayList类型
    ArrayList<Person> list = (ArrayList<Person>)o;
    // 8、遍历ArrayList结合
    for (Person person : list) {
    System.out.println(person);
    }
    ois.close();
    oos.close();
    }
    }
  • 相关阅读:
    RHEL7 timedatectl命令
    广告点击率的贝叶斯平滑
    Expectation Propagation: Theory and Application
    微博推荐算法学习(Weibo Recommend Algolrithm)
    百度技术沙龙第48期回顾:大规模机器学习(含资料下载)
    内容匹配广告投放技术4:网盟CTR预估(百度文库课程)
    Logistic Regression的几个变种
    广告点击率预测 [离线部分]
    GBDT(MART) 迭代决策树入门教程 | 简介
    xgboost: 速度快效果好的boosting模型
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11261547.html
Copyright © 2020-2023  润新知