简体   繁体   English

为什么我应该在Java中使用序列化而不是文件I / O

[英]Why should i use Serialization instead of File I/O in java

在序列化机制中,我们使用objectinputstream和object outputstream将对象写入流中。这些对象通过网络传递。在这种机制中,使用Object输入/输出流。那么我可以使用File INPUT / OUTPUT Streams代替调用序列化标记接口吗? 。

I guess You are mixing up serialization and general I/O. 我猜您正在混淆序列化和常规I / O。

Serialization is a way to transform objects into byte sequences (and back, which is called Deserialization). 序列化是一种将对象转换为字节序列(反过来又称为反序列化)的方法。 This way, You can transmit serializable objects over the network and store them into files. 这样,您可以通过网络传输可序列化的对象并将其存储到文件中。

File input/output streams are for storing/reading any kind of data to/from files. 文件输入/输出流用于向文件存储/从文件读取任何类型的数据。

First let's concentrate on the definition: Serialization: It is the process of converting object state into a format that can be stored and reconstructed later in the same way. 首先让我们集中讨论定义:序列化:这是将对象状态转换为可以稍后以相同方式存储和重建的格式的过程

Whereas in file I/O it can't be possible to store data-structure or object and reconstructed later in the same way. 而在文件I / O中,不可能存储数据结构或对象,以后再以相同的方式进行重构。 That's why we use serialization or database query methods (like sql, mongodb). 这就是为什么我们使用序列化或数据库查询方法(例如sql,mongodb)的原因。

JSON/XML can also be used for serialization using its parser. JSON / XML也可以使用其解析器进行序列化。

Take an example of javascript (not java, but take it like language-agnostics): 以javascript为例(不是Java,但是像语言不可知论):

var obj = { // it's an object in javascript (same like json) a: "something", b: 3, c: "another" };

Now if you try to use file i/o in this to save in a file (say abc.txt), it will be saved as a string which means it can't be accessed later in other code by reading this file (abc.txt) like this: 现在,如果您尝试在其中使用文件I / O保存到文件中(例如abc.txt),它将被保存为字符串,这意味着以后无法通过读取此文件的其他代码来访问它。 txt),如下所示:

// readThisFile();

// obj.a;

But if you use serialization (in javascript using JSON natively), you can read it from the file 但是,如果您使用序列化(在使用JSON的javascript中),则可以从文件中读取序列化

when you need to transfer your object on network, you need to serialized it. 当您需要在网络上传输对象时,需要对其进行序列化。 Below link might be useful for you. 以下链接可能对您有用。

http://java.sun.com/developer/technicalArticles/Programming/serialization/ http://java.sun.com/developer/technicalArticles/Programming/serialization/

File I/O and Serialization are two different things. 文件I / O和序列化是两件事。 File I/O is used to read/write a file. 文件I / O用于读取/写入文件。 Serialization interface is used for binary interpretation of an object. 序列化接口用于对象的二进制解释。 So NO, you can't use File Streams for sending over network.(maybe there is some workaround for sending data over network using file streams, but its like trying to fly with a car) 因此,不,您不能使用文件流通过网络发送数据(也许有一些解决方法可以使用文件流通过网络发送数据,但是就像试图乘汽车飞行一样)

Since streams are additive, you can do something like 由于流是加性的,因此您可以执行以下操作

FileOutputStream fos = new FileOutputStream("/some/file/to/write/to");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(someObject);

Not sure this is what you were asking, but it's hard to tell. 不确定这是您要的内容,但是很难说。

Serialization/Deserialization is used to read and write objects, which not only makes compressed data, which is unreadable but also is writes it in binary. 序列化/反序列化用于读取和写入对象,这不仅使无法读取的压缩数据还以二进制形式写入。 The File I/O is used for reading and writing. 文件I / O用于读取和写入。 It appears that you do not want to serialize, if you don't, well do not use it. 看来您不想序列化,否则请不要使用它。 Read and write your files in text. 读取和写入文本文件。

In serialization mechanism,we write the object into s stream using ObjectInputStream and ObjectOutputStream . 在序列化机制中,我们使用ObjectInputStreamObjectOutputStream将对象写入s流。

Ok

These objects are passed across the network.In this mechanism using a ObjectInput/Output stream. 这些对象通过网络传递。在这种机制中,使用ObjectInput / Output流。

I am following you. 我在跟着你。

So can I use File Input/Output streams instead of calling serialization marker interface?. 那么我可以使用文件输入/输出流而不是调用序列化标记器接口吗?

Here you lost me. 在这里,你失去了我。 Do you mean to send an object over the network or just to serialize it? 您是要通过网络发送对象还是只是对其进行序列化?

Of course you can use whichever Input/Output streams along with ObjectInput/ObjectOutput streams to serialize objects to different media. 当然,您可以使用任何Input / Output流以及ObjectInput / ObjectOutput流将对象序列化到不同的媒体。

For instance: 例如:

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("jedis.bin"));
out.writeObject(new Jedi("Luke"));

Would serialize the object into a file called jedis.bin 将对象序列化为名为jedis.bin的文件

And the code 和代码

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOputStream out = new ObjectOutputStream(byteStream);
out.writeObject(new Jedi("Luke"));

Would serialize the object into a memory array. 将对象序列化到内存阵列中。

So, anything that is an output/input stream is subject of being used as the underlying stream used by ObjectInput/ObjectOutput streams. 因此,任何输出/输入流都将被用作ObjectInput / ObjectOutput流使用的基础流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM