看 effective java program .(java 高效编程).里面解释很详细...
其实java.io.Serializable仅仅是一个空的接口,里面没有任何东西,引入它只是告诉你的程序,这个类可以序列化... ------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
其实java.io.Serializable仅仅是一个空的接口,里面没有任何东西,引入它只是告诉你的程序,这个类可以序列化... ------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;
The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The default mechanism for saving the Object's fields can be invoked by calling out.defaultWriteObject. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput. The readObject method is responsible for reading from the stream and restoring the classes fields. It may call in.defaultReadObject to invoke the default mechanism for restoring the object's non-static and non-transient fields. The defaultReadObject method uses information in the stream to assign the fields of the object saved in the stream with the correspondingly named fields in the current object. This handles the case when the class has evolved to add new fields. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput. Serializable classes that need to designate an alternative object to be used when writing an object to the stream should implement this special method with the exact signature:
ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
This writeReplace method is invoked by serialization if the method exists and it would be accessible from a method defined within the class of the object being serialized. Thus, the method can have private, protected and package-private access. Subclass access to this method follows java accessibility rules. Classes that need to designate a replacement when an instance of it is read from the stream should implement this special method with the exact signatute.
ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
This readResolve method follows the same invocation rules and accessibility rules as writeReplace.
int x; int y;
SubObject so = new SubObject();
..........
}class SubObject implements Serializable{
String s = "abc"; //String is OK, because String is java.io.Serializable
}public class Save{ // in Save.java
public void main(String[] args) {
ObjectYouWannaSave original = new ObjectYouWannaSave();
original.x = -1; original.y = 100; .....
ObjectOutputStream out = null;
try{
out = new ObjectOutputStream(new FileOutputStream(new File("c:/save.dat")));
out.writeObject(original);
}catch(Exceptione){
}finally{
try{out.close();}catch(Exception e){}
}
}
}public class Read{ // in Read.java
public void main(String[] args) {
ObjectInputStream in = null;
try{
out = new ObjectInStream(new FileInStream(new File("c:/save.dat")));
ObjectYouWannaSave o = (ObjectYouWannaSave)in.readObject();
System.out.println("x="+o.x);
System.out.println("subobject.string=" + o.so.s);
........
}catch(Exceptione){
}finally{
try{in.close();}catch(Exception e){}
}
}
}这是个最简单的例子,先运行Save,以后什么时候运行Read都可以(只要c:\save.dat文件还存在就可以了)