![](/img/trans.png)
[英]i get ClassNotFoundException when i try to read from excel file
[英]Get StreamCorruptedException when I try to read cripted object from a file
當我嘗試讀取存儲在文件中的受限制的對象時,顯然一切似乎都可以正常工作, 並且可以獲取已保存的值 ,但是在日志中總是出現此錯誤
java.io.StreamCorruptedException
W/System.err﹕ at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2067)
W/System.err﹕ at java.io.ObjectInputStream.<init>(ObjectInputStream.java:372)
而且我不知道如何解決它
我用它來加載:
ObjectInputStream inputStream = null;
try {
inputStream = new ObjectInputStream(cipherInputStream);
} catch (IOException e) {
e.printStackTrace();
}
if (inputStream != null) {
try {
myDecipheredObject = (Serializable) inputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
和這個存儲
ObjectOutputStream outputStream = null;
try {
outputStream = new ObjectOutputStream(cipherOutputStream);
outputStream.writeObject(object);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (RuntimeException e) {
e.printStackTrace();
}
該行加載方法中發生錯誤
inputStream = new ObjectInputStream(cipherInputStream);
在您指定的行中,我能想到的唯一可能的異常原因是:
當從另一個InputStream構造一個新的ObjectInputStream時,構造函數嘗試從另一個InputStream讀取標頭。 如果無法讀取和驗證此標頭,則會引發StreamCorrupted異常。 如果無法讀取標頭,或者標頭錯誤(例如,包含不同的幻數或版本號),則會發生這種情況。
是否有可能在輸入流嘗試從中讀取之前實際上沒有創建相應的輸出流? 還是兩個不同的線程各自嘗試從cipherOutputStream讀取?
如果對象的寫入需要很長時間,則值得在構造對象后立即刷新outputStream,以便至少立即寫入標頭:
outputStream = new ObjectOutputStream(cipherOutputStream);
outputStream.flush();
outputStream.writeObject(object);
outputStream.close();
最后,我假設您使用的密鑰與用於加密的密鑰相同。 如果不是,則可能導致標題被錯誤地解密。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.