示例代码:将对象持久化为文件,然后再次拿出来。
应用场景:比如一个从晚上抓取的数据保存在一个列表,集合,map结构中,下次想不再联网继续使用,则可以用持久化方法,将包含数据的对象写到一个持久化对象中,下次使用时,在程序初始化时自动加载。
// Serialize today's date to a file.
FileOutputStream f = new FileOutputStream("tmp");
ObjectOutput s = new ObjectOutputStream(f);
s.writeObject("Today");
s.writeObject(new Date());
s.flush();
// Deserialize a string and date from a file.
FileInputStream in = new FileInputStream("tmp");
ObjectInputStream s = new ObjectInputStream(in);
String today = (String)s.readObject();
Date date = (Date)s.readObject();