除了Writing&之外,使用Serializable从文件中读取对象 [英] Use of Serializable other than Writing& Reading object to/from File

查看:17
本文介绍了除了Writing&之外,使用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.

许多不错的读物:

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

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