我有一本Book類和一個擴展Book的Library Book類。我將信息存儲在隨機訪問文件中。我有一個writeToFile方法,它將Book對象寫入隨機訪問文件。我的LibraryBook類的方法writeToFile調用super.writeToFile,然後我希望它將特定於LibraryBook的字段寫入文件。什麼是正確的方法來做到這一點?看到代碼:使用超類和擴展類方法寫入隨機訪問文件
public void writeToFile(String fileName, long location) throws Exception {
try {
super.writeToFile(fileName, location);
RandomAccessFile invFile = new RandomAccessFile(fileName, "rw");
// seek to correct record in file
invFile.seek(location);
// now write out the record
// write out the data in fixed length fields
// String fields must be truncated if too large
// or padded with blanks if too small
//write library book variables to file
invFile.writeLong(branchID);
//etc....
invFile.close();
} catch (FileNotFoundException notFound) {
throw new FileNotFoundException();
} catch (IOException io) {
throw io;
}
}
我怎麼可以編寫它,以便LibraryBook將writeToFile方法可以調用:
public void writeToFile(String fileName, long location) throws Exception {
try {
RandomAccessFile invFile = new RandomAccessFile(fileName, "rw");
// seek to correct record in file
invFile.seek(location);
// now write out the record
// write out the data in fixed length fields
// String fields must be truncated if too large
// or padded with blanks if too small
//write out all Book variable to file
invFile.writeLong(ISBN);
//etc.....
} catch (FileNotFoundException notFound) {
throw new FileNotFoundException();
} catch (IOException io) {
throw io;
}
}
從延伸圖書的LibraryBook類方法:
從書類的方法超類方法並將LibraryBook保存到文件中?
請把你的代碼示例縮短到所需的最低限度...(見http://sscce.org) –
我縮短了代碼示例 – trs