程序:HashMap<String, String> map=new HashMap<String,String>();
map.put("name", "kwj");
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(baos);
oos.writeObject(map);
oos.flush();
String str=baos.toString("GBK");
byte[] byte_array=str.getBytes("GBK");
oos.close();
baos.close();
ByteArrayInputStream bais=new ByteArrayInputStream(byte_array);
ObjectInputStream ois=new ObjectInputStream(bais);
HashMap<String, String> m1=(HashMap<String, String>) ois.readObject();
出错:Exception in thread "main" java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2325)
at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:3063)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2864)
at java.io.ObjectInputStream.readUTF(ObjectInputStream.java:1072)
at java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:704)
------解决方案--------------------
HashMap<String, String> map=new HashMap<String,String>();
map.put("name", "kwj");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(map);
oout.flush();
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
System.out.println(bin.available());
byte[] b = new byte[bin.available()];
bin.read(b);
HashMap<String, String> m1 = (HashMap<String, String>)(new ObjectInputStream(new ByteArrayInputStream(b)).readObject());
System.out.println(m1.get("name"));