对象序列化的应用,一个文件中存储多个对象

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 block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (oos != null) {
					oos.flush();
					oos.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.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 block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (ois != null) {

					ois.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.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);
		}
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值