Java Reference
In-Depth Information
public static void writeRecord(RandomAccessFile fi le)
throws IOException
//First fi nd starting byte for current record…
long fi lePos = (acctNum-1) * REC_SIZE;
//Position fi le pointer…
fi lePos);
//Now write the four (fi xed-size) fi elds.
//Note that a defi nition must be provided
//for method writeString…
fi le.writeLong(acctNum);
writeString(fi le, surname, SURNAME_SIZE);
writeString(fi le, initials, NUM_INITS);
fi le.writeFloat(balance);
public static void writeString(RandomAccessFile fi le,
String text, int fi xedSize) throws IOException
int size = text.length();
if (size<=fi xedSize)
fi le.writeChars(text);
//Now 'pad out' the fi eld with spaces…
for (int i=size; i<fi xedSize; i++)
fi le.writeChar(' ');
else //String is too long!
fi le.writeChars(text.substring(0,fi xedSize));
//Write to fi le the fi rst fi xedSize characters of
//string text, starting at byte zero.
public static void showRecords(RandomAccessFile fi le)
throws IOException
long numRecords = fi le.length()/REC_SIZE;
fi; //Go to start of fi le.
for (int i=0; i<numRecords; i++)
acctNum = fi le.readLong();
surname = readString(fi le, SURNAME_SIZE);
Search WWH ::

Custom Search