package file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import hashmap2.Good;
public class TestSer {
/**
* 序列化 存对象的过程 实现Serializable接口的都可以序列化
* @param args
* @throws IOException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
Good good = new Good(1009,"派大星",20,"kg",50);
//实现Serializable,并创建对象
ArrayList<Good> list = new ArrayList<Good>();
list.add(good);
//创建对象输出流
FileOutputStream fos = new FileOutputStream("d:\\b.ext");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//写对象
oos.writeObject(list);
//关闭数据流
oos.close();
fos.close();
//反序列化
FileInputStream fis = new FileInputStream("d:\\b.ext");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Good> good2 = (ArrayList<Good>)ois.readObject();
ois.close();
fis.close();
//遍历输出
for(Good o : good2){
o.show();
}
}
}