Thinking in java 4th Edition 读书笔记-I/O(6)
2009-04-15 16:59
441 查看
1.(Questions)
Even opening the file and reading in the object mystery requires the Class object for Alien;
2.When b1 is recovered, the Blip1 default constructor is called. This is different from recovering a Serializable object, in which the object is constructed entirely from its stored bits, with no constructor calls.
3.
4.One way to prevent sensitive parts of your object from being serialized is to implement your class as Externalizable, as shown previously. Then nothing is automatically serialized, and you can explicitly serialize only the necessary parts inside writeExternal( ).
If you’re working with a Serializable object, however, all serialization happens automatically. To control this, you can turn off serialization on a field-by-field basis using the transient keyword
Since Externalizable objects do not store any of their fields by default, the transient keyword is for use with Serializable objects only.
5.As long as you’re serializing everything to a single stream, you’ll recover the same web of objects that you wrote, with no accidental duplication of objects.
put all the objects that comprise the state of your system in a single container and simply write that container out in one operation. Then you can restore it with a single method call as well.
6.It’s as if the statics didn’t get serialized at all! That’s right—even though class Class is Serializable, it doesn’t do what you expect. So if you want to serialize statics, you must do it yourself.
You can see that they are explicitly called as part of the storage and retrieval process.
ObjectInputStream in = new ObjectInputStream( new FileInputStream(new File("..", "X.file"))); Object mystery = in.readObject();
Even opening the file and reading in the object mystery requires the Class object for Alien;
2.When b1 is recovered, the Blip1 default constructor is called. This is different from recovering a Serializable object, in which the object is constructed entirely from its stored bits, with no constructor calls.
3.
import java.io.*; import static net.mindview.util.Print.*; public class Blip3 implements Externalizable { private int i; private String s; // No initialization public Blip3() { print("Blip3 Constructor"); // s, i not initialized } public Blip3(String x, int a) { print("Blip3(String x, int a)"); s = x; i = a; // s & i initialized only in non-default constructor. } public String toString() { return s + i; } public void writeExternal(ObjectOutput out) throws IOException { print("Blip3.writeExternal"); // You must do this: out.writeObject(s); out.writeInt(i); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { print("Blip3.readExternal"); // You must do this: s = (String)in.readObject(); i = in.readInt(); } public static void main(String[] args) throws IOException, ClassNotFoundException { print("Constructing objects:"); Blip3 b3 = new Blip3("A String ", 47); print(b3); ObjectOutputStream o = new ObjectOutputStream( new FileOutputStream("Blip3.out")); print("Saving object:"); o.writeObject(b3); o.close(); // Now get it back: ObjectInputStream in = new ObjectInputStream( new FileInputStream("Blip3.out")); print("Recovering b3:"); b3 = (Blip3)in.readObject(); print(b3); } }
4.One way to prevent sensitive parts of your object from being serialized is to implement your class as Externalizable, as shown previously. Then nothing is automatically serialized, and you can explicitly serialize only the necessary parts inside writeExternal( ).
If you’re working with a Serializable object, however, all serialization happens automatically. To control this, you can turn off serialization on a field-by-field basis using the transient keyword
Since Externalizable objects do not store any of their fields by default, the transient keyword is for use with Serializable objects only.
5.As long as you’re serializing everything to a single stream, you’ll recover the same web of objects that you wrote, with no accidental duplication of objects.
put all the objects that comprise the state of your system in a single container and simply write that container out in one operation. Then you can restore it with a single method call as well.
6.It’s as if the statics didn’t get serialized at all! That’s right—even though class Class is Serializable, it doesn’t do what you expect. So if you want to serialize statics, you must do it yourself.
You can see that they are explicitly called as part of the storage and retrieval process.
相关文章推荐
- Thinking in java 4th Edition 读书笔记-Concurrency(2)
- Thinking in java 4th Edition 读书笔记-I/O(1)
- Thinking in java 4th Edition 读书笔记-I/O(3)
- Thinking in java 4th Edition 读书笔记-Concurrency(1)
- Thinking in java 4th Edition 读书笔记-I/O(4)
- Thinking in java 4th Edition 读书笔记-I/O(5)
- Thinking in java 4th Edition 读书笔记-Concurrency(3)
- 【资源】Thinking in java 4th edition 英文版 高清晰pdf+源代码+练习题答案
- Thinking in java 4th Edition 读书笔记-I/O(2)
- 对 Thinking in java 4th Edition I/O DirList.java的疑问
- Thinking in Java 4th Edition Source Code
- Thinking in Java 4th edition 中文非扫描版
- 不好意思, java的英文书换成thinking in java 4th edition
- thinking in java 4th 读书笔记
- Thinking in Java 4th Edition (Bruce Eckel) java编程思想 书上例题源代码
- 《Fundamentals of Project Management 4th Edition》 读书笔记 第十三章 项目经理领导力
- Thinking in Java 读书笔记 (一)
- 读书笔记 Think in Java 4th 第十一章 持有对象 List、Set、Queue和Map
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅲ
- 【Thinking in Java】读书笔记