Java ReadObject方法
要检查ObjectInputStream类的方法readObject是否已完成文件读取,而不是捕获其抛出的异常 如果没有,我怎样才能写出newmast.writeObject(accountRecord);在这种情况下达成的声明Java ReadObject方法,java,exception-handling,objectinputstream,Java,Exception Handling,Objectinputstream,要检查ObjectInputStream类的方法readObject是否已完成文件读取,而不是捕获其抛出的异常 如果没有,我怎样才能写出newmast.writeObject(accountRecord);在这种情况下达成的声明 // read oldmast.ser try { while (true) { accountRecord = (AccountRecord) inOldmast.readObject(); //
// read oldmast.ser
try {
while (true) {
accountRecord = (AccountRecord) inOldmast.readObject();
//read trans.ser
while (true) {
transactionRecord = (TransactionRecord) inTrans.readObject();
if (transactionRecord.getAccountNumber() == accountRecord.getAccount()) {
accountRecord.combine(transactionRecord);
}//end if
}//end inner while
outNewmast.writeObject(accountRecord);
}//end while
}//end try
catch (ClassNotFoundException e) {
System.err.println("Error reading file.");
System.exit(1);
}//end catch
catch (IOException e) {
System.err.println("Error reading file.");
System.exit(1);
}//end catch
是,检查输入流以查看是否还有其他可用内容:
是,检查输入流以查看是否还有其他可用内容:
最好的办法是事先序列化元素的数量,这样您就可以:
cnt = file.readInt();
for (int i=0;i<cnt;i++) {
file.readObject();
}
最好的办法是事先序列化元素的数量,这样您就可以:
cnt = file.readInt();
for (int i=0;i<cnt;i++) {
file.readObject();
}
TransactionRecord readRecord(ObjectInputStream stream) throws OptionalDataException, IOException {
try {
transactionRecord = (TransactionRecord) stream.readObject();
} catch (OptionalDataException e) {
if (e.eof) {
return null;
} else {
throw e;
}
}
return transactionRecord;
}
.....
TransactionRecord record;
while ((record = readRecord(inTrans)) != null) {
doSomethingWithRecord(record);
}
endOfFile();