Java Reference
In-Depth Information
Remember, we are keeping the index in order by part number. After the third record ( FLT015 ) is read and stored,
the index will be this:
BLJ375 2
FLT015 3
PKL070 1
After the fourth record ( DKP080 ) is read and stored, the index will be this:
BLJ375 2
DKP080 4
FLT015 3
PKL070 1
Finally, after the fifth record ( GSF555 ) is read and stored, the index will be this:
BLJ375 2
DKP080 4
FLT015 3
GSF555 5
PKL070 1
Program P7.8 illustrates how an index can be created as described.
Program P7.8
import java.io.*;
import java.util.*;
public class CreateIndex {
static final int StringFixedLength = 20;
static final int PartNumSize = 6;
static final int PartRecordSize = 64;
static final int MaxRecords = 100;
static final String EndOfData = "END";
public static void main(String[] args) throws IOException {
RandomAccessFile fp = new RandomAccessFile("parts.bin", "rw");
Index[] index = new Index[MaxRecords + 1];
createMasterIndex(index, fp);
saveIndex(index);
printIndex(index);
fp.close();
} //end main
public static void createMasterIndex(Index[] index,
RandomAccessFile f) throws IOException {
Scanner in = new Scanner(new FileReader("parts.txt"));
int numRecords = 0;
Part part = getPartData(in);
while (part != null) {
int searchResult = search(part.partNum, index, numRecords);
Search WWH ::




Custom Search