Java Reference
In-Depth Information
showRecord(ranAccts); //Defi ned below.
System.out.print("\nEnter new balance: ");
balance = input.nextFloat();
//Get rid of carriage return!
currentPos = ranAccts.getFilePointer();; //Back 4 bytes.
"\nModify another balance (y/n)? ");
reply = (input.nextLine()).toLowerCase();
}while (reply.equals("y"));
//(Alternative to method in previous example.)
public static void showRecord(RandomAccessFile fi le)
throws IOException
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