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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:37:07  来源:igfitidea点击:

How to Serialize primitive types

javaserialization

提问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 DataOutputStreamclass and deserialized using DataInputStreamclass

基本类型可以使用序列化DataOutputStream类和使用反序列化DataInputStream

回答by ncmathsadist

All of the primitive types have write methods in the class ObjectOutputStream. You can serialize any primitive via an ObjectOutputStreamin this manner. For details see the Java API page for ObjectOutputStream.

所有原始类型在类中都有写方法ObjectOutputStream。您可以通过ObjectOutputStream这种方式序列化任何原语。有关详细信息,请参阅 Java API 页面ObjectOutputStream

回答by user207421

Make sure they are neither transientnor static.

确保它们既不是 也不transientstatic