Java序列化对象与非序列化对象
问题描述
1)非序列化的java对象是否可以通过网络发送,由另一个JVM执行或存储在本地文件存储中以恢复数据?
2)序列化和存储java对象与存储java对象而不序列化之间的区别是什么?
- Can a non-serialised java object be sent over the network to be executed by another JVM or stored in local file storage to get the data restored?2) What is the difference between serialising and storing the java object vs storing the java object without serialising it?
推荐答案
序列化是一种将java对象表示为一系列字节的方法。它只是一种格式。
内置java序列化是一个类,它提供用于将java对象转换为一系列字节的API。而已。当然,反序列化是一个补充过程,允许将这个二进制流转换回对象。
Serialization is a way to represent a java object as a series of bytes. Its just a format nothing more. A “build-in” java serialization is a class that provides an API for conversion of the java object to a series of bytes. That’s it. Of course, deserialization is a “complementary” process that allows to convert this binary stream back to the object.
序列化/反序列化本身与发送无关通过网络的事情。它只是方便发送一个二进制流,可以通过序列化从对象创建。
The serialization/deserialization itself has nothing to do with the “sending over the network” thing. Its just convenient to send a binary stream that can be created from the object with the serialization.
更多的是,有时内置的序列化不是获取的最佳方式二进制流,因为有时可以通过使用更少的字节来转换对象。
Even more, sometimes the built-in serialization is not an optimal way to get the binary stream, because sometimes the object can be converted by using less bytes.
所以你可以使用自定义协议,为序列化提供自己的自定义(例如,)
So you can use you’re custom protocol, provide your own customization for serialization (for example, Externalizable)
甚至使用第三方库,如
or even use third party libraries like Apache Avro
我认为这有效地回答了你的两个问题:
I think this effectively answers both of your questions:
你可以转非序列化对象(我猜是那个没有实现Serializable接口的对象)如果你想要自己的字节序列(字节流),然后通过网络发送,存储在二进制文件中,无论如何。
当然,您必须了解如何阅读这种二进制格式以便转换回来。
You can turn the non-serialized object (I guess the one that doesn’t implement “Serializable” interface) to the series of bytes (byte stream) by yourself if you want and then send it over the network, store in a binary file, whatsoever.Of course you’ll have to understand how to read this binary format for converting back.
因为序列化只是转换协议而不是一个存储相关的东西,答案是显而易见的。
- 点赞
- 收藏
- 关注作者
评论(0)