0

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 ?

Ahoura Ghotbi
  • 2,866
  • 12
  • 36
  • 65

2 Answers2

1

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.

BlackJoker
  • 3,099
  • 2
  • 20
  • 27
  • but is the above code reliable? what if a value is larger than 15 bytes? could you explain how exactly java knows what the next value is? if the record shows `1 name lastname 10.00`, how would the code above read it? – Ahoura Ghotbi Apr 04 '13 at 15:31
  • It depends on how the file is generated,If each record is stored as 4byte+15byte+15byte+8byte,they will be read correctly – BlackJoker Apr 04 '13 at 15:35
  • ohh ok, that was the confusing part, so you have to take bytes into account to be able to locate a value correct? – Ahoura Ghotbi Apr 04 '13 at 15:36
0

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, e.g. 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.

linski
  • 5,046
  • 3
  • 22
  • 35