Java Reference
In-Depth Information
showRecord(ranAccts); //Defi ned below.
System.out.print("\nEnter new balance: ");
balance = input.nextFloat();
input.nextLine();
//Get rid of carriage return!
currentPos = ranAccts.getFilePointer();
ranAccts.seek(currentPos-4); //Back 4 bytes.
ranAccts.writeFloat(balance);
System.out.print(
"\nModify another balance (y/n)? ");
reply = (input.nextLine()).toLowerCase();
}while (reply.equals("y"));
//(Alternative to method in previous example.)
ranAccts.close();
}
public static void showRecord(RandomAccessFile fi le)
throws IOException
{
fi le.seek((acctNum-1)*REC_SIZE);
acctNum = fi le.readLong();
surname = readString(fi le, SURNAME_SIZE);
initials = readString(fi le, NUM_INITS);
balance = fi le.readFloat();
System.out.println("Surname: " + surname);
System.out.println("Initials: " + initials);
System.out.printf("Balance: %.2f %n",balance);
}
public static String readString(
RandomAccessFile fi le, int fi xedSize)
throws IOException
{
//Set up empty buffer before reading from fi le…
StringBuffer buffer = new StringBuffer();
for (int i=0; i<fi xedSize; i++)
//Read character from fi le and append to buffer.
buffer.append(fi le.readChar());
return buffer.toString(); //Convert into String.
}
}
Search WWH ::




Custom Search