对象序列化的应用,一个文件中存储多个对象
来源:互联网 发布:淘宝神笔在哪里打开 编辑:程序博客网 时间:2024/06/04 23:11
import java.io.Serializable;public class Xuliehualianxi01 implements Serializable {private int id;private String name;private int count;private Double price;public Xuliehualianxi01(int id, String name, int count, Double price) {super();this.id = id;this.name = name;this.count = count;this.price = price;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}}
import java.io.*;import java.util.ArrayList;import java.util.List;public class Xulianxi01 {public static void serial(List<Xuliehualianxi01> list) {ObjectOutputStream oos = null;String dox = "F:/IOtest/xuliehua/save.txt";try {oos = new ObjectOutputStream(new FileOutputStream(dox));oos.writeObject(list);System.out.println("录入新订单成功!");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {if (oos != null) {oos.flush();oos.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static List<Xuliehualianxi01> unserial() {ObjectInputStream ois = null;List<Xuliehualianxi01> list = new ArrayList<>();Xuliehualianxi01 x = null;String dox = "F:/IOtest/xuliehua/save.txt";try {ois = new ObjectInputStream(new FileInputStream(dox));//list=(ArrayList<Xuliehualianxi01>) ois.readObject();// 用下面方式,追加对象序列,用上面的方式,是覆盖,相当于一个新的对象。list.addAll((ArrayList<Xuliehualianxi01>)ois.readObject());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {if (ois != null) {ois.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return list;}public static void main(String[] args) {File f = null;List<Xuliehualianxi01> list1 = new ArrayList<>();List<Xuliehualianxi01> list2 = new ArrayList<>();double money = 0.0;list2.add(new Xuliehualianxi01(1, "卫生纸", 10, 45.0));list2.add(new Xuliehualianxi01(2, "水杯", 2, 34.0));list2.add(new Xuliehualianxi01(3, "电脑", 1, 4565.0));list2.add(new Xuliehualianxi01(4, "键盘", 10, 792.0));list2.add(new Xuliehualianxi01(5, "手机", 3, 6288.0));f = new File("F:/IOtest/xuliehua/save.txt");if (f.exists()) {list1 = unserial();for (int i = 0; i < list1.size(); i++) {System.out.println("用户买了" + list1.get(i).getCount() + "个" + list1.get(i).getName() + ",价格为" + list1.get(i).getCount() * list1.get(i).getPrice() + "元");money += list1.get(i).getCount() * list1.get(i).getPrice();}System.out.println("用户总共需要付款:" + money + "元");} else {serial(list2);}}}
阅读全文
0 0
- 对象序列化的应用,一个文件中存储多个对象
- Java序列化 如何把多个对象存储在一个文件中
- 文件复制、对象可序列化存储
- 完成序列化和反序列化的一个集合操作,集合对象中要求保存2个用户对象
- 五、BDB JE中对序列化对象的存储
- android中对象序列化存储
- 使用Java序列化把对象存储到文件中,再从文件中读出来
- 使用Java序列化把对象存储到文件中,再从文件中读出来
- 使用Java序列化把对象存储到文件中,再从文件中读出来
- Java中如何序列化一个对象
- Java 中如何序列化一个对象
- Java 中如何序列化一个对象
- Java中如何序列化一个对象
- php memcache 对象序列化,存储对象的方法
- 对象序列化中多次写入文件头的问题
- LINUX应用技巧,序列化存储 Python 对象
- 关于Java对象序列化的应用~~~
- AS3对象序列化存储
- 《为什么人工智能肯定会超越人类?》
- ZXing生成二维码(Java)
- muduo源码学习(18)-EventLoopThread
- Change Internal ID of ODI Repository. (ODI-10093: You are importing an object from another)
- 18、oracle:存储过程
- 对象序列化的应用,一个文件中存储多个对象
- Velocity.js的使用
- 17AHU排位赛1 D题(线段树除2)
- 剑指offer_链表---复杂链表的复制
- TCP 拥塞控制和流量控制
- quartz_properties文件配置之主要配置
- python库学习笔记——re库:正则表达式入门(一)
- Android数据存储(二)---Sqlite数据库(上)
- 开发人员必备技能:单元测试