下面是一个示例代码,其中List
,String
和Map
已被序列化和反序列化。可以说我需要通过电汇发送file
。接收方客户如何知道反序列化按照List,String,Map的顺序?他应该怎么知道要读取的对象是什么?如何在不知道序列化内容的情况下读取ObjectInputStream?
public static void serializeBunchOfObjects() throws FileNotFoundException, IOException {
List<String> foo = new ArrayList<String>();
String str = "foo";
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("foo"));
oos.writeObject(foo);
oos.writeObject(str);
oos.writeObject(map);
oos.close();
}
public static void deserializeBunchOfObjects() throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("foo"));
List<String> foo = (List)ois.readObject();
String str = (String) ois.readObject();
Map<Integer, Integer> mapper = (Map) ois.readObject();
}
“?应该知道,反序列化接收客户端如何在列表中,字符串,地图的顺序”问题就没有意义了。客户如何知道它是一个对象流?客户端如何知道对象正在被发送* *任何客户端 - 服务器系统都有一个应用协议,并且两端都应该实现它。 – EJP