java 如何序列化原始类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6611737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Serialize primitive types
提问by BOSS
So is there a way to achieve serializing the primitive data?
那么有没有办法实现原始数据的序列化呢?
EDIT Any Specific Example will be great.
编辑任何特定的例子都会很棒。
回答by BOSS
Primitive types can be serialized using DataOutputStream
class and deserialized using DataInputStream
class
基本类型可以使用序列化DataOutputStream
类和使用反序列化DataInputStream
类
回答by ncmathsadist
All of the primitive types have write methods in the class ObjectOutputStream
. You can serialize any primitive via an ObjectOutputStream
in this manner. For details see the Java API page for ObjectOutputStream
.
所有原始类型在类中都有写方法ObjectOutputStream
。您可以通过ObjectOutputStream
这种方式序列化任何原语。有关详细信息,请参阅 Java API 页面ObjectOutputStream
。
回答by user207421
Make sure they are neither transient
nor static
.
确保它们既不是 也不transient
是static
。