Serialize an object to a file
Suppose we have a class called Queue.class. We want to save the state of the Queue in a file. Since our Queue extends the Vector class, the methods needed to serialize the object are already done. All we need is an input or output stream.
First the Queue class import java.util.Vector;
import java.io.*; public class Queue extends Vector {
/*
** FIFO, first in first out
*/
Queue() {
super();
}
void put(Object o) {
addElement(o);
} Object get() {
if (isEmpty()) return null;
Object o = firstElement();
removeElement(o);
return o;
} Object peek() {
if (isEmpty()) return null;
return firstElement();
}
}
To serialize (save the Queue state to a file) : public static void main(String args[]) {
Queue theQueue;
theQueue = new Queue();
theQueue.put("element 1");
theQueue.put("element 2");
theQueue.put("element 3");
theQueue.put("element 4");
System.out.println(theQueue.toString());
// serialize the Queue
System.out.println("serializing theQueue");
try {
FileOutputStream fout = new FileOutputStream("thequeue.dat");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(theQueue);
oos.close();
}
catch (Exception e) { e.printStackTrace(); }
}
To unserialize (to load a previously saved Queue) : public static void main(String args[]) {
Queue theQueue;
theQueue = new Queue();
// unserialize the Queue
System.out.println("unserializing theQueue");
try {
FileInputStream fin = new FileInputStream("thequeue.dat");
ObjectInputStream ois = new ObjectInputStream(fin);
theQueue = (Queue) ois.readObject();
ois.close();
}
catch (Exception e) { e.printStackTrace(); }
System.out.println(theQueue.toString());
Suppose we have a class called Queue.class. We want to save the state of the Queue in a file. Since our Queue extends the Vector class, the methods needed to serialize the object are already done. All we need is an input or output stream.
First the Queue class import java.util.Vector;
import java.io.*; public class Queue extends Vector {
/*
** FIFO, first in first out
*/
Queue() {
super();
}
void put(Object o) {
addElement(o);
} Object get() {
if (isEmpty()) return null;
Object o = firstElement();
removeElement(o);
return o;
} Object peek() {
if (isEmpty()) return null;
return firstElement();
}
}
To serialize (save the Queue state to a file) : public static void main(String args[]) {
Queue theQueue;
theQueue = new Queue();
theQueue.put("element 1");
theQueue.put("element 2");
theQueue.put("element 3");
theQueue.put("element 4");
System.out.println(theQueue.toString());
// serialize the Queue
System.out.println("serializing theQueue");
try {
FileOutputStream fout = new FileOutputStream("thequeue.dat");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(theQueue);
oos.close();
}
catch (Exception e) { e.printStackTrace(); }
}
To unserialize (to load a previously saved Queue) : public static void main(String args[]) {
Queue theQueue;
theQueue = new Queue();
// unserialize the Queue
System.out.println("unserializing theQueue");
try {
FileInputStream fin = new FileInputStream("thequeue.dat");
ObjectInputStream ois = new ObjectInputStream(fin);
theQueue = (Queue) ois.readObject();
ois.close();
}
catch (Exception e) { e.printStackTrace(); }
System.out.println(theQueue.toString());
class A implements Serializable
{
...;
}
A 就可以序列化了.不要实现什么方法.write
OutputStream out = getOutStream();//可以是socket,文件等IO流
out.writeObject(a); //就可以write,否则就出NotSerializableException
存的时候是递归的存所有成员变量.read
InputStream in = getInputStream();
a = in.readObject(a);当然,一个大的类有时不需要存所有的成员变量,有两个方法解决
(1)在不需要序列话的成员变量前加transient关键字.
(2)Class A 不实现Serializable,实现Externalizable,能实现最大的控制权
自己来写 writeExternal ,readExternal
我在我的项目里全用Externalizable,自由度大.该存的存,不该存的不存.
Serialize相关的概念就是Stream,说起来就麻烦了,说实话有时间发贴子,还不如看书,看jdk source很有收获的.
太谢谢了!!!外层该有个类实现Serialize类吧,你看,对么?
public class TestSerializable implements Serializable {
public static void main(String args[]) {
Queue theQueue;
theQueue = new Queue();
theQueue.put("element 1");
theQueue.put("element 2");
theQueue.put("element 3");
theQueue.put("element 4");
System.out.println(theQueue.toString());
// serialize the Queue
System.out.println("serializing theQueue");
try {
FileOutputStream fout = new FileOutputStream("thequeue.dat");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(theQueue);
oos.close();
}
catch (Exception e) { e.printStackTrace(); }
}}