Java Reference
In-Depth Information
}
String getDesc()
{
return desc;
}
String getPartnum()
{
return partnum;
}
int getQty()
{
return qty;
}
int getUnitCost()
{
return ucost;
}
}
}
Listing 8-6 ' s PartsDB class implements the java.io.Closeable interface so
thatitcanbeusedinthecontextofthetry-with-resourcesstatement (see Chapter3 ) .I
couldhavechosentoimplement Closeable 's java.lang.AutoCloseable su-
perinterface,butchose Closeable insteadbecauseits close() methodisdeclared
to throw IOException .
PartsDB declaresconstantsthatidentifythelengthsofthestringand32-bitinteger
fields.Itthendeclaresaconstantthatcalculatestherecordlengthintermsofbytes.The
calculation takes into account the fact that a character occupies two bytes in the file.
These constants are followed by a field named raf that's of type RandomAc-
cessFile . This field is assigned an instance of the RandomAccessFile class in
thesubsequentconstructor,whichcreates/opensanewfileoropensanexistingfilebe-
cause of "rw" .
PartsDB next declares append() , close() , numRecs() , select() , and
update() .Thesemethodsappendarecordtothefile,closethefile,returnthenumber
of records in the file, select and return a specific record, and update a specific record:
Search WWH ::




Custom Search