簡體   English   中英

“ DataOutputStream”和“ ObjectOutputStream”之間的區別

[英]difference between “DataOutputStream” and “ObjectOutputStream”

我是遵循此Java教程的初學者程序員。

在“ 基本I / O”部分中,提到的兩個類是Data StreamsObject 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用於對象(以及它們的序列化),那么我應該使用哪一個? 這兩個都使用基本類型的示例類之間沒有明顯的區別。 我通常也使用原始類型。

對於性能,哪個更好? 還有其他大差異嗎?

謝謝。

DataStreams用於基本類型的 I / O,這些類型intfloatdouble等。

ObjectStreams用於對象的 I / O。

如果您知道要顯式地使用基本類型,請使用DataStreams ,否則請使用更通用的ObjectStreams ,它們實現了DataInput接口以及ObjectInput接口,因此可以使用基本體和對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM