“对象序列化”是什么意思?你能举例解释一下吗?
当前回答
我将提供一个类比,以潜在地帮助巩固对象序列化/反序列化的概念目的/实用性。
I imagine object serialization/deserialization in the context of attempting to move an object through a storm drain. The object is essentially "decomposed" or serialized into more modular versions of itself - in this case, a series of bytes - in order to effectively be granted passage through a medium. In a computational sense, we could view the path traveled by the bytes through the storm drain as being akin to bytes traveling through a network. We're transmuting our object in order to conform to a more desirable mode of transportation, or format. The serialized object will typically be stored in a binary file which may later be read from, written to, or both.
也许一旦我们的对象能够作为分解的字节序列通过drain,我们可能希望将对象的表示形式作为二进制数据存储在数据库或硬盘驱动器中。不过,主要的要点是,通过序列化/反序列化,我们可以选择让对象在序列化后保持二进制形式,或者通过执行反序列化来“检索”对象的原始形式。
其他回答
我喜欢@OscarRyz的礼物方式。虽然在这里我是在继续这个最初由@amitgupta写的连载故事。
即使知道机器人的类结构和序列化的数据,地球的科学家也不能反序列化的数据,可以使机器人工作。
Exception in thread "main" java.io.InvalidClassException:
SerializeMe; local class incompatible: stream classdesc
:
火星的科学家们正在等待全额付款。一旦付款完成,火星的科学家与地球的科学家分享了序列号。地球科学家把它设置成机器人级别,一切都好了。
敢于回答这个6年前的问题,为Java新手增加了非常高级的理解
什么是序列化?
将对象转换为字节
什么是反序列化?
将字节转换回对象(反序列化)。
什么时候使用序列化?
当我们想持久化对象时。 当我们希望对象在JVM生命周期之后仍然存在时。
现实世界的例子:
ATM:当账户持有人试图通过ATM从服务器取款时,提现明细等账户持有人信息将被序列化并发送到服务器,服务器将这些明细反序列化并用于操作。
在java中如何执行序列化。
实现java.io.Serializable接口(标记接口,所以没有方法实现)。 持久化对象:使用java.io.ObjectOutputStream类,这是一个过滤器流,它是底层字节流的包装器(将object写入文件系统或通过网络传输扁平对象并在另一端重新构建)。
writeObject(<<instance>>) -写入一个对象 readObject()——读取一个序列化的对象
记住:
序列化对象时,只保存对象的状态,不保存对象的类文件或方法。
当你序列化一个2字节的对象时,你会看到51字节的序列化文件。
步骤如何序列化和反序列化对象。
答案:它如何转换为51字节的文件?
First writes the serialization stream magic data (STREAM_MAGIC= "AC ED" and STREAM_VERSION=version of the JVM). Then it writes out the metadata of the class associated with an instance (length of the class, the name of the class, serialVersionUID). Then it recursively writes out the metadata of the superclass until it finds java.lang.Object. Then starts with the actual data associated with the instance. Finally writes the data of objects associated with the instance starting from metadata to the actual content.
你也可以在这里查看我的Youtube视频解释
编辑:阅读的参考链接。
这将回答一些常见的问题:
How not to serialize any field in the class. Ans: use transient keyword When child class is serialized does parent class get serialized? Ans: No, If a parent is not extending the Serializable interface parents field don't get serialized. When a parent is serialized does child class get serialized? Ans: Yes, by default child class also gets serialized. How to avoid child class from getting serialized? Ans: a. Override writeObject and readObject method and throw NotSerializableException. b. also you can mark all fields transient in child class. Some system-level classes such as Thread, OutputStream, and its subclasses, and Socket are not serializable.
Serialization is the process of saving an object in a storage medium (such as a file, or a memory buffer) or to transmit it over a network connection in binary form. The serialized objects are JVM independent and can be re-serialized by any JVM. In this case the "in memory" java objects state are converted into a byte stream. This type of the file can not be understood by the user. It is a special types of object i.e. reused by the JVM (Java Virtual Machine). This process of serializing an object is also called deflating or marshalling an object.
要序列化的对象必须实现java.io.Serializable Interface。 对象的默认序列化机制写入对象的类、类签名以及所有非瞬态和非静态字段的值。
class ObjectOutputStream extends java.io.OutputStream implements ObjectOutput,
ObjectOutput接口扩展了DataOutput接口,并添加了用于序列化对象和向文件写入字节的方法。ObjectOutputStream扩展了java.io.OutputStream并实现了ObjectOutput接口。它将对象、数组和其他值序列化到流中。因此ObjectOutputStream的构造函数被写成:
ObjectOutput ObjOut = new ObjectOutputStream(new FileOutputStream(f));
上面的代码已用于使用ObjectOutputStream()构造函数创建ObjectOutput类的实例,该构造函数将FileOuputStream的实例作为参数。
ObjectOutput接口用于实现ObjectOutputStream类。构造ObjectOutputStream是为了序列化对象。
在java中反序列化对象
与序列化相反的操作称为反序列化,即从一系列字节中提取数据称为反序列化,也称为膨胀或解组。
ObjectInputStream扩展了java.io.InputStream,实现了ObjectInput接口。它反序列化来自输入流的对象、数组和其他值。因此ObjectInputStream的构造函数被写成:
ObjectInputStream obj = new ObjectInputStream(new FileInputStream(f));
上面的程序代码创建了ObjectInputStream类的实例,以反序列化已被ObjectInputStream类序列化的文件。上面的代码使用FileInputStream类的实例创建实例,FileInputStream类包含必须反序列化的指定文件对象,因为ObjectInputStream()构造函数需要输入流。
将文件返回为Object: http://www.tutorialspoint.com/java/java_serialization.htm
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("/tmp/employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/employee.ser");
}catch(IOException i)
{
i.printStackTrace();
}
}
}
import java.io.*;
public class DeserializeDemo
{
public static void main(String [] args)
{
Employee e = null;
try
{
FileInputStream fileIn = new FileInputStream("/tmp/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);
}
}
序列化是在内存中获取一个“活动”对象,并将其转换为可以存储在某处的格式(例如。在内存中,在磁盘上),然后“反序列化”回一个活动对象。
推荐文章
- 如何分割逗号分隔的字符串?
- Java字符串—查看字符串是否只包含数字而不包含字母
- Mockito.any()传递带有泛型的接口
- 在IntelliJ 10.5中运行测试时,出现“NoSuchMethodError: org.hamcrest. matcher . descripbemismatch”
- 使用String.split()和多个分隔符
- Java数组有最大大小吗?
- 在Android中将字符串转换为Uri
- 从JSON生成Java类?
- 为什么java.util.Set没有get(int index)?
- Swing和AWT的区别是什么?
- 为什么Java流是一次性的?
- 四舍五入BigDecimal *总是*有两位小数点后
- 设计模式:工厂vs工厂方法vs抽象工厂
- Java:检查enum是否包含给定的字符串?
- 它的意思是:序列化类没有声明一个静态的最终serialVersionUID字段?