缓冲流问题 (ByteArrayInputStream/DataInputStream)
import java.io.*;
public class TestBuffer2{
public static void main(String args [])throws Exception{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeInt(1);
dos.writeUTF("HELLO");
byte [] buf = new byte [1024];
ByteArrayInputStream bis = new ByteArrayInputStream(buf);
DataInputStream dis = new DataInputStream(bis);
System.out.println(dis.readInt());
System.out.println(dis.readUTF());
dos.flush();
dos.close();
dis.close();
}
}
为什么 读出的数据居然是 0 和空(什么都没有 一些空行)呢 请大虾们帮帮忙了
------解决方案--------------------
API
Java code
public ByteArrayOutputStream() {
this(32);
}
public ByteArrayOutputStream(int size) {
if (size < 0) {
throw new IllegalArgumentException("Negative initial size: "
+ size);
}
buf = new byte[size];
}