Section 14 Serialization

Save object to file:

	FileOutputStream fileStream = new FileOutputStream("output.out");
	ObjectOutputStream os = new ObjectOutputStream(fileStream);
	os.writeObject(serilaziableObject);
	os.close();


Connection stream:

Low-level Represent connections to destinations and sources such as files or network sockets.

Chain stream:

Work only if chained to other streams. Can be called method on.


During serialization, the object's instance variables are saved.

Serialization saves the entire object graph. All objects referenced by instance variables are also serialized.

 
Serialize interface is known as a marker or tag interface, which doesn't have any methods to implement. It is an announcement that it is saveable through the serialization mechanism.

If any superclass of a class is serializable, the subclass is automatically serializable.


Serialization is all or nothing. The entire object graph should be serialized correctly or fail.

Mark an instance variable as transient if it can't (shouldn't) be saved. Transient says don't save this variable during serialization, just skip it. 


Static variables are not saved in file. Their values are the current class values after the class is first loaded in JVM.

Don't make serializable objects dependent on a dynamicallly-changing static variable! It might not be the same when the object comes back.

You can't save network connections, thread, or file objects. They are all depend on (and specific to) a particular runtime 'experience'. They are instantiated in a way that's unique to a particular run of your program, on a particular platform, in a particular JVM.

Class is not saved. Classnotfound.

Objects are saved in order.


Deserialization:

FileInputStream fileStream = new FileInputStream("input.in");
ObjectInputStream os = new ObjectInputStream(fileStream);
Object one = os.readObject();
Box box = (Box)one;
os.close();

During deserialization, the class of all objects in the graph must be available to the JVM. The JVM must have these classes.

Instance variables are given the values from the serialized state. Transient variables are given default values.

Objects are deserialized in the same order as they are saved.


File:

1. Make a file object.

File f = new File("code.txt");

2. Make a new directory.

File dir = new FIle("Dir");
dir.mkdir();

3. List the content of a directory

if(dir.isDirectory()) {
    String[] dirContents = dir.list();
} 

4. Absolute path of a file/directory

dir.getAbsolutePath();

5. Delete

boolean isDeleted = f.delete();

Write to a file:

1. Without buffer

FileWriter writer = new FileWriter("fo.txt");
writer.write("hellow!");
writer.close();

2. With buffer

BufferedWriter writer = new BufferedWriter(new FileWriter("fo.txt"));
writer.write("hello!");
writer.close();
First method writes to the file every time you call write.

Second method writes the the file when the buffer is full or you call flush().


Read from a file:

BufferedReader reader = new BufferedReader(new FileReader("fo.txt"));
String line = null;
while((line = reader.readLine()) != null) {
    // line
}
reader.close();

serialVersionUID:

The serialized object should be able to deserialized and is compatible with its class version in current JVM. 

The compiler checks the compatibility based on the serialVersionUID. 

You take responsibility for making sure that the serialized object is compatible with the class declared in current JVM.

Otherwise Deserialization exception?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值