Java序列化------我的学习-----坚持看英文文档

      Java中提供了一种机制,叫做对象的序列化。它说的是一个对象可以被描述为一个字节序列,这个字节序列不仅仅包含了这个对象的数据,还包含了一些其他的信息:对象的类型、存储在对象中的数据的类型。

      当一个序列化的对象被写入到一个文件后,就可以从文件中读取出来,也就是反序列化。类型信息、字节数和数据组合起来可以用来重新在内存中生成一个对象。

      最令人印象深刻的是,整个过程是与JVM无关的,这就意味在这个平台上序列化后在另外一个完全不同的平台上可以反序列化。

      ObjectInputStream和ObjectOutputStream类包含了序列化和反序列化的方法。

      ObjectOutputStream包含了许多为不同类型写的方法,但是有一个方法特别的突出。

             public final void writeObject(Object x) throws IOException

             上面的方法序列化一个对象然后将它发送到输出流中。

      类似地,ObjectInputStream类包含了下面的反序列一个对象的方法。

             public final Object readObject() throws IOException,ClassNotFoundException

             这个方法检索出下一个对象流和反序列化它。返回的值的类型是Object型的,所以你需要将它转换为适当的数据类型。

 


 

下面来看一个实例,在这里我就不在翻译了,还是以英文的方式显示:

To demonstrate how serialization works in Java, I am going to use the Employee class that we discussed early on in the book. Suppose that we have the following Employee class, which implements the Serializable interface:

public class Employee implements java.io.Serializable
{
public String name;
public String address;
public int transient SSN;
public int number;

public void mailCheck()
{
System.out.println("Mailing a check to " + name + " " + address);
}

}



Notice that for a class to be serialized successfully, two conditions must be met:

  • The class must implement the java.io.Serializable interface.

  • All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.

If you are curious to know if a Java Satandard Class is serializable or not, check the documentation for the class. The test is simple: If the class implements java.io.Serializable, then it is serializable; otherwise, it's not.

Serializing an Object:

The ObjectOutputStream class is used to serialize an Object. The following SerializeDemo program instantiates an Employee object and serializes it to a file.

When the program is done executing, a file named employee.ser is created. The program does not generate any output, but study the code and try to determine what the program is doing.

Note: When serializing an object to a file, the standard convention in Java is to give the file a .ser extension.

import java.io.*;

public class SerializeDemo
{
public static void main(String [] args)
{



Employee e = new Employee();
e.name = "Reyan Ali";
e.address = "Phokka Kuan, Ambehta Peer";
e.SSN = 11122333;
e.number = 101;

try
{
FileOutputStream fileOut =new FileOutputStream("employee.ser");
ObjectOutputStream out =new ObjectOutputStream(fileOut);(1)
out.writeObject(e);
out.close();
fileOut.close();
}catch(IOException i)
{
i.printStackTrace();
}
}
}



 


 

(1)处的作用是创建了一个指定的对象输出流,也就是创建出来的用于将对象转换为流的对象out是一个特殊的对象,它调用writeObject(Object o)就可以把序列化流写入到文件流中,它之所以有这个功能是因为你给它传递了一个文件流参数,从而它就具有了这个功能。

ObjectOutputStream (Java 2 Platform SE 6)

ObjectOutputStream

public ObjectOutputStream



(OutputStream
out) throws IOException


创建写入指定 OutputStream 的 ObjectOutputStream。此构造方法将序列化流部分写入底层流;调用者可以通过立即刷新流,确保在读取头部时,用于接收 ObjectInputStreams 构造方法不会阻塞。

如果安装了安全管理器,则在通过重写 ObjectOutputStream.putFields 或 ObjectOutputStream.writeUnshared 方法的子类的构造方法来直接或间接调用此构造方法时,它将对 "enableSubclassImplementation" SerializablePermission 进行检查。

 

参数:
out - 要写入数据的输出流
抛出:
IOException - 如果在写入流部分时发生 I/O 错误
SecurityException - 如果不受信任的子类非法重写安全敏感方法
NullPointerException - 如果 outnull

 

 


 

Deserializing an Object:

The following DeserializeDemo program deserializes the Employee object created in the SerializeDemo program. Study the program and try to determine its output:

import java.io.*;



public class DeserializeDemo

{
public static void main(String [] args)
{
Employee e = null;

try
{
FileInputStream fileIn =new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println(.Employee class not found.);
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
}



This would produce following result:

Deserialized Employee...
Name: Reyan Ali
Address:Phokka Kuan, Ambehta Peer
SSN: 0
Number:101

Here are following important points to be noted:

  • The try/catch block tries to catch a ClassNotFoundException, which is declared by the readObject() method. For a JVM to be able to deserialize an object, it must be able to find the bytecode for the class. If the JVM can't find a class during the deserialization of an object, it throws a ClassNotFoundException.

  • Notice that the return value of readObject() is cast to an Employee reference.

  • The value of the SSN field was 11122333 when the object was serialized, but because the field is transient, this value was not sent to the output stream. The SSN field of the deserialized Employee object is 0.

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值