java的ObjectInputStream的readUTF()报EOFException异常
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeUTF("aaa");
byte [] bytes = bo.toByteArray();
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(bi);
String str = (String) oi.readUTF();
System.out.println(str);
以上写法会出现EOFException异常,但是我把writeUTF改成writeObject就好了,这是为什么,正确代码如下:
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject("aaa");
byte [] bytes = bo.toByteArray();
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(bi);
String str = (String) oi.readObject();
System.out.println(str);