简体   繁体   English

从randomaccessfile读取数据

[英]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. 我必须能够从randomaccessfile中读取一份我的考试,并且上面的代码有些令人困惑。

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 . 我们接收file ,并从filereadInt并将其分配给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 . 接下来,我想我们创建一个大小为15的新字节,并将文件中的名字读入该字节,然后分配给firstname now here is what I dont understand, what does readFully do? 现在这是我不了解的内容, readFully做什么? and how does the code above knows to move on to the next value for lastName . 以及上面的代码如何知道要移至lastName的下一个值。 So simply put, 简而言之,

byte b1[] = new byte[ 15 ];
      file.readFully( b1 );
      firstName = new String( b1, 0 );
      firstName = firstName.trim();

VS 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: 正如api所说:

Reads b.length bytes from this file into the byte array, starting at the current file pointer. 从当前文件指针开始,将b.length个字节从此文件读取到字节数组中。 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 从指定的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) 帐户-4B(整数大小)
  • first name - 15B 名-15B
  • last name - 15B 姓-15B
  • balance - 8B 天平-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 : 至于RandomAccessFile.readFullyMethod

Reads b.length bytes from this file into the byte array, starting at the current file pointer. 从当前文件指针开始,将b.length个字节从此文件读取到字节数组中。 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 . 该方法将阻塞,直到 读取请求的字节数,检测到流的末尾或引发异常 为止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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