本文介绍了“DataOutputStream”之间的差异和“ObjectOutputStream”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名初学程序员,遵循此。

I'm a beginner programmer following this Java Tutorial.

在部分,提到的两个类是和。

In the Basic I/O section, two of the classes mentioned are Data Streams and Object Streams.

非常类似地使用它们:

out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));

// ..

in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));

DataInputStream

out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));

// ..

in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dataFile)));

ObjectInputStream

我知道它说 DataInputStreams 用于原始对象, ObjectInputStreams 是用于对象(及其序列化),我应该使用哪一个?两个使用原始类型的示例类之间没有明显的区别。我通常也使用原始类型。

I know it says that DataInputStreams are used for primitive objects, and ObjectInputStreams are used for objects (and serialization of them), so which one should I use? It isn't a noticeable difference between the two example classes which both use primitive types. I usually use primitive types, too.

对于性能,哪一个更好?还有其他大的差异吗?

For performance, which one is better? and are there any other large differences?

谢谢。

推荐答案

DataStreams 用于原语类型的的I / O, int float double 等等。

DataStreams are used for I/O of primitive types which are int, float, double and so on.

ObjectStreams 用于对象的I / O

如果你知道你将明确使用原始类型,那么使用 DataStreams ,否则使用更通用的 ObjectStreams 实现以及因此可以使用基元和对象。

If you know you're going to be explicitly working with primitive types then use DataStreams, otherwise go with the more generic ObjectStreams which implement the DataInput interface as well as ObjectInput interface so can work with primitives as well as objects.

这篇关于“DataOutputStream”之间的差异和“ObjectOutputStream”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:55