使用Serializable而非Writing&从File读取对象 [英] Use of Serializable other than Writing& Reading object to/from File

查看:97
本文介绍了使用Serializable而非Writing&从File读取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪些情况下使用除了Writing& amp;之外的工具序列化是一个很好的编码实践。从文件中读取对象。在一个项目中,我通过了代码。使用implements serializable的类,即使在该类/项目中没有任何写入/读取对象到/从文件?

In Which Cases it is a good coding practice to use implements serializable other than Writing & Reading object to/from file.In a project i went through code. A class using implements serializable even if in that class/project no any Writing/Reading objects to/from file?

推荐答案

如果对象离开了创建它的JVM,那么该类应该实现Serializable。

序列化是一种方法,通过该方法,对象可以表示为包含对象数据的字节序列,以及有关对象类型和存储的数据类型的信息。对象。

Serialization is a method by which an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

将序列化对象写入文件后,可以从文件中读取并反序列化,即表示对象的类型信息和字节并且它的数据可用于在内存中重新创建对象。

After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

这是反序列化的主要目的。从对象的书面(松散地)表示中获取对象信息,对象类型,变量类型信息。因此首先需要序列化,以实现这一目标。

This is the main purpose of de-serialization. To get the object information, object type, variable type information from a written(loosely speaking) representation of an object. And hence serialization is required in the first place, to make this possible.

因此,无论何时,只要您的对象有可能离开JVM,程序就是正在执行,你应该创建类,实现Serializable。

读取/写入对象到文件(内存),或通过Internet或任何方式传递对象其他类型的连接。每当对象离开创建它的JVM时,它应该实现Serializable,这样一旦它回到另一个/同一个JVM就可以被序列化和反序列化以进行识别。

Reading/Writing objects into files (Memory), or passing an object over internet or any other type of connection. Whenever the object, leaves the JVM it was created in, it should implement Serializable, so that it can be serialized and deserialized for recognition once it enters back into another/same JVM.

许多好读物:

  • 1: Why Java needs Serializable interface?
  • 2: What is the purpose of Serialization in Java?

这篇关于使用Serializable而非Writing&从File读取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆