“DataOutputStream”和“ObjectOutputStream”之间的java差异
我是一个初学者程序员,学习这个Java Tutorial
在Basic I/O部分中,提到的两个类是Data Streams和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
用于对象(以及它们的序列化),那么我应该使用哪一个呢?两个都使用基元类型的示例类之间没有明显的区别。我通常也使用基本类型
就性能而言,哪一个更好?还有其他大的区别吗
谢谢
# 1 楼答案
DataStreams
用于基本类型的I/O,这些基本类型是int
、float
、double
等等ObjectStreams
用于对象的I/O如果您知道要显式地处理基元类型,那么使用
DataStreams
,否则使用更通用的ObjectStreams
,它实现了DataInput interface和ObjectInput interface,因此可以处理基元和对象