//所有要序列化的类都必须实现Serializable接口Dog d=new Dog("来福1","公",23);File file = new File("f:\图片\dog1");FileOutputStream fos = new FileOutputStream(file);BufferedOutputStream bos = new BufferedOutputStream(fos);ObjectOutputStream oos = new ObjectOutputStream(bos);oos.writeObject(d);//关闭必须由内而外!oos.close();bos.close();fos.close();System.out.println("ok");
反序列化:将对象从硬盘介质中还原到内存中File file = new File("f:\图片\dog");FileInputStream fis = new FileInputStream(file);BufferedInputStream bis = new BufferedInputStream(fis);ObjectInputStream ois = new ObjectInputStream(bis);Dog d = (Dog)ois.readObject();System.out.println(d.getName());ois.close(); bis.close();fis.close();//保存1000只狗 ArrayList