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);
}
}
}