简体   繁体   中英

reading data from a randomaccessfile

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 ?

As the api says:

Reads b.length bytes from this file into the byte array, starting at the current file pointer. This method reads repeatedly from the file until the requested number of bytes are read.

    public void write(RandomAccessFile file) throws IOException {
    file.writeInt(account);
    byte b1[] = new byte[15];
    // set firstname to b1
    file.write(b1);
    byte b2[] = new byte[15];
    // set lastname to b2
    file.write(b2);
    double balance =123.1;
    file.writeDouble(balance);
}

if you generate the file exactly like that above,you read progress will be ok.

If this is a school/university exam, bear in mind that they know to have absurdities on purpose, to confuse you.

Considering a comment from the code you posted:

Read a record from the specified RandomAccessFile

I'd bet that it is defined that the binary file you are reading from has data stored in records like this:

  • account - 4B (size of int)
  • first name - 15B
  • last name - 15B
  • balance - 8B

I also bet that it is supposed that the data is written correctly - meaning exactly in the above order and length. If it is not, eg there is a string instead of the integer, or the first name is bigger, it will probably result in error or misinterpreted data - that's why your snippets give different results.

It is a binary file you are reading from anyway, if you want to read something out if it, you must now the exact format in which it was written. Otherwise it's like deciphering an alien letters.

As for the RandomAccessFile.readFullyMethod :

Reads b.length bytes from this file into the byte array, starting at the current file pointer. This method reads repeatedly from the file until the requested number of bytes are read . This method blocks until the requested number of bytes are read, the end of the stream is detected, or an exception is thrown .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM