怎样对带有不可序列化属性的Java对象进行序列化
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样对带有不可序列化属性的Java对象进行序列化相关的知识,希望对你有一定的参考价值。
1、序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。2、可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题。
3、序列化的实现:
1)、将需要被序列化的类实现Serializable接口,该接口没有需要实现的方法,implements Serializable只是为了标注该对象是可被序列化的
2)、然后使用一个输出流(如:FileOutputStream)来构造一个ObjectOutputStream(对象流)对象
3)、接着,使用ObjectOutputStream对象的writeObject(Object obj)方法就可以将参数为obj的对象写出(即保存其状态),要恢复的话则用输入流。 参考技术A 出于很多原因我们想使用自定义的序列化方法取代Java默认的机制。一个最常见的原因是提高性能,而另一个原因是有时候我们无法使用默认的序列化方法。在这篇文章中,我们具体来讨论怎样通过定制的序列化方法,对一个较大的、带有不可序列化属性的对象进行序列化。
下面这段代码定义了一个简单的类。它可以把一个给定的对象序列化到一个指定的文件,或者从相同的文件中把对象反序列化出来。
1
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 package dustin.examples.serialization; import static java.lang.System.out; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * Simple serialization/deserialization demonstrator. * * @author Dustin */ public class SerializationDemonstrator /** * Serialize the provided object to the file of the provided name. * @param objectToSerialize Object that is to be serialized to file; it is * best that this object have an individually overridden toString() * implementation as that is used by this method for writing our status. * @param fileName Name of file to which object is to be serialized. * @throws IllegalArgumentException Thrown if either provided parameter is null. */ public static <T> void serialize(final T objectToSerialize, final String fileName) if (fileName == null) throw new IllegalArgumentException( "Name of file to which to serialize object to cannot be null."); if (objectToSerialize == null) throw new IllegalArgumentException("Object to be serialized cannot be null."); try (FileOutputStream fos = new FileOutputStream(fileName); ObjectOutputStream oos = new ObjectOutputStream(fos)) oos.writeObject(objectToSerialize); out.println("Serialization of Object " + objectToSerialize + " completed."); catch (IOException ioException) ioException.printStackTrace(); /** * Provides an object deserialized from the file indicated by the provided * file name. * * @param <T> Type of object to be deserialized. * @param fileToDeserialize Name of file from which object is to be deserialized. * @param classBeingDeserialized Class definition of object to be deserialized * from the file of the provided name/path; it is recommended that this * class define its own toString() implementation as that will be used in * this method's status output. * @return Object deserialized from provided filename as an instance of the * provided class; may be null if something goes wrong with deserialization. * @throws IllegalArgumentException Thrown if either provided parameter is null. */ public static <T> T deserialize(final String fileToDeserialize, final Class<T> classBeingDeserialized) if (fileToDeserialize == null) throw new IllegalArgumentException("Cannot deserialize from a null filename."); if (classBeingDeserialized == null) throw new IllegalArgumentException("Type of class to be deserialized cannot be null."); T objectOut = null; try (FileInputStream fis = new FileInputStream(fileToDeserialize); ObjectInputStream ois = new ObjectInputStream(fis)) objectOut = (T) ois.readObject(); out.println("Deserialization of Object " + objectOut + " is completed."); catch (IOException | ClassNotFoundException exception) exception.printStackTrace(); return objectOut; 参考技术B 序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题。序列化的实现:将需要被序列化的类实现Serializable接口,该接口没有需要实现的方法,implements Serializable只是为了标注该对象是可被序列化的,然后使用一个输出流(如:FileOutputStream)来构造一个ObjectOutputStream(对象流)对象,接着,使用ObjectOutputStream对象的writeObject(Object obj)方法就可以将参数为obj的对象写出(即保存其状态),要恢复的话则用输入流。
以上是关于怎样对带有不可序列化属性的Java对象进行序列化的主要内容,如果未能解决你的问题,请参考以下文章
Java序列化,碰到serialVersionUID不一致怎么处理?
Qt持久性对象进行序列化(同时比较了MFC与Java的方法)