如果我寫我這樣的代碼,它提供了一個錯誤「找不到文件訪問被拒絕......」如何序列化Java對象 - 將對象作爲InputStream
public class ImplRegistration implements IRegistration {
@Override
public boolean newRegistration(Registration_BE reg_be) {
FileOutputStream fos = new FileOutputStream("serial.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(reg_be);
}
}
出於安全原因,我將代碼的第四行更改爲:
FileOutputStream fos = new FileOutputStream("f://serial.ser");
但是之後它顯示了異常java.io.NotSerializableException: java.io.ByteArrayInputStream
。
如何序列化對象?
確保你的對象序列化實現了'Serializable'接口。此外,如果您共享有問題的代碼併發布堆棧跟蹤而不是錯誤消息,那將會很好。 –
我使用Windows 8,所以即使我給c://我也顯示訪問被拒絕。 – sabarirajan
'C:/'總是會讓你拒絕訪問。請注意,在Windows中,路徑應該像'f:\\\\ some \\ path',或者再次使用'/ some/path'。 –