Java Reference
In-Depth Information
Listing 8-6. Implementing the parts flat file database
import java.io.Closeable;
import java.io.IOException;
import java.io.RandomAccessFile;
class PartsDB implements Closeable
{
final static int PNUMLEN = 20;
final static int DESCLEN = 30;
final static int QUANLEN = 4;
final static int COSTLEN = 4;
private
final
static
int
RECLEN
=
2*PNUMLEN+2*DESCLEN+QUANLEN+COSTLEN;
private RandomAccessFile raf;
PartsDB(String pathname) throws IOException
{
raf = new RandomAccessFile(pathname, "rw");
}
void append(String partnum, String partdesc, int qty,
int ucost)
throws IOException
{
raf.seek(raf.length());
write(partnum, partdesc, qty, ucost);
}
@Override
public void close() throws IOException
{
//
throw new IOException("cannot close raf");
raf.close();
}
int numRecs() throws IOException
{
return (int) raf.length()/RECLEN;
}
Part select(int recno) throws IOException
 
Search WWH ::




Custom Search