反序列化对象时出现以下异常:
java.io.OptionalDataException
java.io.ObjectInputStream.readObject0(Unknown Source)
java.io.ObjectInputStream.readObject(Unknown Source)
java.util.HashSet.readObject(Unknown Source)
sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
我用来序列化和反序列化对象的代码如下:
public class ObjectToFile
{
public static void save(Object obj, String name)
{
FileOutputStream fos;
try
{
ODebug.Write(Level.INFO, "SER-0049: Saving objects from file...");
fos = new FileOutputStream(".//data//" + name);
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos));
oos.writeObject(obj);
oos.close();
fos.close();
}
catch (Exception e)
{
ODebug.Write(Level.SEVERE, "SER-0000: Caught exception in save()", e);
}
}
public static Object read(String name)
{
FileInputStream fis;
Object obj;
try
{
ODebug.Write(Level.INFO, "SER-0049: Reading objects from file...");
fis = new FileInputStream(".//data//" + name);
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(fis));
obj = ois.readObject();
ois.close();
fis.close();
return obj;
}
catch (Exception e)
{
ODebug.Write(Level.SEVERE, "SER-0001: Caught exception in read()", e);
}
return null;
}
}
我试图序列化/反序列化的对象是一个哈希图,如下所示:
public static HashMap<Integer, Document> ProductList = new HashMap<>();
Document 类具有以下定义和一些方法。
public class Document implements Serializable
{
private static final long serialVersionUID = 1L;
public Integer documentID;
public DocumentZone titleZone = new DocumentZone();
public DocumentZone bodyZone = new DocumentZone();
}
请帮忙!