当前位置: 首页 > news >正文

Java下对象的序列化和反序列化(写出和读入)

代码如下:

public class MyWork {public static void main(String[] args) throws IOException, ClassNotFoundException {//序列化File f = new File("testFile/testObject.txt");ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));ArrayList<Student> list1 = new ArrayList<>();Student s1 = new Student("张三",18);Student s2 = new Student("李四",19);Student s3 = new Student("王武",20);list1.add(s1);list1.add(s2);list1.add(s3);oos.writeObject(list1);oos.close();//反序列化ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));ArrayList<Student> list2 = (ArrayList<Student>) ois.readObject();Iterator<Student> it = list2.iterator();while(it.hasNext()){Student stu = it.next();System.out.println(stu.getName() + "======" + stu.getAge());}ois.close();}
}
http://www.lryc.cn/news/179329.html

相关文章:

  • 基于springboot的洗衣店订单管理系统
  • Llama2部署踩坑
  • Adams齿轮副
  • Elasticsearch keyword 中的 ignore_above配置项
  • RabbitMQ原理(一):基础知识
  • [Linux]Git
  • ChatGPT终于可以进行网络搜索 内容不再限于2021年9月前
  • uni-app:实现页面效果1
  • 归一化和标准化的联系与区别及建议
  • 数据结构--栈的实现
  • 第十章 异常
  • Rust冒泡排序
  • 麒麟信安服务器操作系统V3.5.2重磅发布!
  • 密码技术 (1) - 对称密码
  • 基于PYQT5的GUI开发系列教程【二】QT五个布局的介绍与运用
  • Cadence PCB 焊盘和封装
  • 正在等待操作系统重新启动。 请重新启动计算机以安装autocad 2024。
  • Windows电脑显示部分功能被组织控制
  • Django模板加载与响应
  • Python 内置函数详解 (4) 类型转换
  • SpringBoot之Web原生组件注入
  • [每周一更]-(第64期):Dockerfile构造php定制化镜像
  • 若依不分离+Thymeleaf select选中多个回显
  • OCX 添加方法和事件 HTML调用ocx函数及回调 ocx又调用dll VS2017
  • 苹果iPhone手机使用草柴返利APP查询领取淘宝天猫京东优惠券如何取消关闭粘贴商品链接时的弹窗提示?
  • 主机安装elasticsearch后无法登陆
  • 【面试题精讲】JavaSe和JavaEE的区别
  • React 全栈体系(十五)
  • 【逆向】(c++)分析pe结构,拉伸pe结构,缩小pe结构
  • PyTorch实战:常用卷积神经网络搭建结构速览