![](/img/trans.png)
[英]What's the difference between DataOutputStream and ObjectOutputStream?
[英]difference between “DataOutputStream” and “ObjectOutputStream”
我是遵循此Java教程的初學者程序員。
在“ 基本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
用於對象(以及它們的序列化),那么我應該使用哪一個? 這兩個都使用基本類型的示例類之間沒有明顯的區別。 我通常也使用原始類型。
對於性能,哪個更好? 還有其他大差異嗎?
謝謝。
DataStreams
用於基本類型的 I / O,這些類型為int
, float
, double
等。
ObjectStreams
用於對象的 I / O。
如果您知道要顯式地使用基本類型,請使用DataStreams
,否則請使用更通用的ObjectStreams
,它們實現了DataInput接口以及ObjectInput接口,因此可以使用基本體和對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.