Here is the function I have :
// Read a record from the specified RandomAccessFile
public void read( RandomAccessFile file ) throws IOException {
account = file.readInt();
byte b1[] = new byte[ 15 ];
file.readFully( b1 );
firstName = new String( b1, 0 );
firstName = firstName.trim();
byte b2[] = new byte[ 15 ];
file.readFully( b2 );
lastName = new String( b2, 0 );
lastName = lastName.trim();
balance = file.readDouble();
}
I have to be able to read from a randomaccessfile for one of my exams and the code above is a bit confusing.
here is what I am guessing is happening, would appreciate it if you could correct me if I am wrong.
we take file
in and readInt
from the file and assign it to account
. next I guess we create a new byte of size 15 and read the first name from the file into the byte and then assigned to firstname
. now here is what I dont understand, what does readFully
do? and how does the code above knows to move on to the next value for lastName
.
So simply put,
byte b1[] = new byte[ 15 ];
file.readFully( b1 );
firstName = new String( b1, 0 );
firstName = firstName.trim();
VS
byte b2[] = new byte[ 15 ];
file.readFully( b2 );
lastName = new String( b2, 0 );
lastName = lastName.trim();
balance = file.readDouble();
Why doesnt it give the same value? are we guessing how long each value (firstname, lastname etc) is ?