簡體   English   中英

如何使用DataInputStream的方法讀取DataOutputStream.writeBytes(String)寫入的數據?

[英]How to use the method of DataInputStream to read data writed by DataOutputStream.writeBytes(String)?

如何使用DataInputStream的方法讀取DataOutputStream.writeBytes(String)寫入的數據?

我真的不知道這個問題!

writeBytes(String)不包括寫入長度的任何指示,因此,除非您提前輸出了長度,否則無法知道要讀取多少。

另請注意, writeBytes(String)僅將writeBytes(String)中每個字符的低八位寫入高位,而這可能不是您想要的。

如果要寫出String查看writeUTF ,它確實包含字符串長度,並且可以使用DataInputStream.readUTF讀取。

你可以建立你的DataOutputStreamByteArrayOutputStream和您的DataInputStreamByteArrayInputStream使用作為緩沖來自字節數組ByteArrayOutputStream ,例如:

ByteArrayOutputStream inMemory = new ByteArrayOutputStream ();
DataOutputStream out = new DataOutputStream (inMemory);

DataInputStream in = new DataInputStream (new ByteArrayInputStream  (inMemory.toByteArray()));

暫無
暫無

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

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