Java Reference
In-Depth Information
//readString defi ned below.
initials = readString(fi le, NUM_INITS);
balance = fi le.readFloat();
System.out.printf("" + acctNum
+ " " + surname
+ " " + initials + " "
+ "%.2f %n",balance);
}
}
public static String readString(
RandomAccessFile fi le, int fi xedSize)
throws IOException
{
String value = ""; //Set up empty string.
for (int i=0; i<fi xedSize; i++)
//Read character and concatenate it onto value…
value+=fi le.readChar();
return value;
}
}
Note that methods readString and writeString above may be used without modifi -
cation in any Java program that needs to transfer strings from/to a random access fi le.
The following screenshot demonstrates the operation of this program (Fig. 4.3 ).
Fig. 4.3 Creating a simple random access fi le and displaying its contents
Search WWH ::




Custom Search