![](/img/trans.png)
[英]objectInputStream.readObject() throws exception java.io.OptionalDataException
[英]ObjectInputStream readobject doesnt work OptionalDataException
當我使用readObject時,我得到OptionalDataException(當流中的下一個元素是原始數據時,試圖讀取一個對象),如何解決此問題? 頁面是可序列化的。 writeObject有效。
public Map<Long,Page<byte[]>> readAllPages(){
ObjectInputStream in = null;
try
{
in= new ObjectInputStream(new FileInputStream(HardDisk.getDefault_File_Name()));
@SuppressWarnings("unchecked")
Map<Long, Page<byte[]>> readMap = (Map<Long, Page<byte[]>>)in.readObject(); // exception here
return readMap;
}
catch(Exception ex)
{
ex.printStackTrace();
System.exit(0);
return null;
}
finally
{
if(in != null)
{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void writeAllPages(Map<Long,Page<byte[]>> hd){
ObjectOutputStream out = null;
try
{
out = new ObjectOutputStream(new FileOutputStream(HardDisk.getDefault_File_Name()));
out.writeObject(hd);
}
catch(Exception ex)
{
ex.printStackTrace();
System.exit(0);
}
finally
{
if(out != null)
{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
問題是具有read方法的類擴展了ObjectInputStream,因此我也不應創建“ in”對象,而是刪除了該對象並將其與“ this”交換,從而解決了問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.