简体   繁体   English

RandomAccessFile-返回空字符串

[英]RandomAccessFile - returning null string

I have a fixed format file. 我有一个固定格式的文件。

I want to access specific lines in this file based on line numbers. 我想根据行号访问此文件中的特定行。

eg read line 100 例如读第100行

The length of each line is 200 bytes. 每行的长度为200个字节。

So directly moving cursor to 100th line using RandomAccessFile would be like: 因此,使用RandomAccessFile将光标直接移动到第100行将是:

File f = new File(myFile);
RandomAccessFile r = new RandomAccessFile(f,"rw");
r.skipBytes(200 * 99);   // linesize * (lineNum - 1)
System.out.println(r.readLine());

However, I am getting output as null. 但是,我得到的输出为null。

What am I missing here ? 我在这里想念什么?

The question is in continuation to answer to my previous question Reaching a specific line in a file using RandomAccessFile 问题是在继续回答我以前的问题,使用RandomAccessFile到达文件中的特定行

Update: 更新:

Below program works exactly as I am expecting: 下面的程序完全符合我的预期:

Line size is 200 characters. 行大小为200个字符。

File f = new File(myFile);
RandomAccessFile r = new RandomAccessFile(f,"rw");
r.seek(201 * (lineNumber-1));   // linesize * (lineNum - 1)
System.out.println(r.readLine());

Giving linenumber (any line number from entire file) is printing that line. 提供行号(整个文件中的任何行号)都将打印该行。

@EJP: Please explain! @EJP:请解释!

RandomAccessFile.readLine() returns null at end of file. RandomAccessFile.readLine()在文件末尾返回null。 As it says in the Javadoc. 正如Javadoc中所说。

So you are at end of file. 因此,您处于文件结尾。

So your calculations that say otherwise are incorrect. 因此,您的计算结果不正确。

Firstly, if you are only going to be reading from the file, not writing to it, then I would suggest that you open the RandomAccessFile as "r" rather than "rw". 首先,如果您只打算从文件中读取而不是写入文件,那么我建议您以“ r”而不是“ rw”打开RandomAccessFile。 The reason is that, if the file is open to allow write access, you can actually move the pointer in the file to a location that is larger than the length of the file, because you can potentially write the file as large as you want. 原因是,如果打开了文件以允许写访问,则实际上可以将文件中的指针移动到大于文件长度的位置,因为您可能会写入所需大小的文件。

For example, if you have a file of 100 bytes in length, opening it in read-only mode will force you to keep the pointer somewhere in those 100 bytes. 例如,如果您有一个100字节长的文件,则以只读模式打开它会迫使您将指针保持在这100字节的某个位置。 However, if you open the same file in read-write mode, you can tell RandomAccessFile to seek(250) and it will obey without any issues, as it thinks that you potentially want to write data out at this point in the file. 但是,如果您以读写模式打开同一文件,则可以告诉RandomAccessFile seek(250),它将服从于任何问题,因为它认为您此时可能希望将数据写出到文件中。

So, as some others have stated, its possible that you have moved past the end of the file, which would be quite valid in "rw" mode. 因此,正如其他人所指出的那样,您可能已经移出了文件末尾,这在“ rw”模式下将非常有效。

Secondly, if you are going to be reading from the beginning of the file, I would recommend that you use seek() rather than skipBytes(). 其次,如果要从文件的开头进行读取,我建议您使用seek()而不是skipBytes()。 By using seek(), you are guaranteeing that you are moving to the exact location you want to be, as it is always relative to the start of the file. 通过使用seek(),可以保证您将移动到想要的确切位置,因为它始终是相对于文件开头的。 However, skipBytes() is relative to the current pointer position in the file, so if you accidentally happened to move the pointer somewhere in your code between the RandomAccessFile constructor and the skipBytes() method call, you wouldn't arrive at the correct spot. 但是,skipBytes()相对于文件中的当前指针位置,因此,如果您不小心将指针移到了RandomAccessFile构造函数和skipBytes()方法调用之间的代码中的某个位置,您将无法到达正确的位置。

So, if you are going to be working from the beginning of the file, seek() provided a greater level of security that you are going to end up at the correct place in the file, no matter what else you do to the RandomAccessFile. 因此,如果您要从文件的开头开始工作,seek()可以提供更高的安全性,无论您对RandomAccessFile进行什么操作,它都将最终位于文件中的正确位置。

What EJP said is correct: http://docs.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.html EJP所说的是正确的: http ://docs.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.html

  • confirm that you have to correct file. 确认您必须更正文件。 Creating a RandomAccessFile with a non-existing file will throw an exception. 使用不存在的文件创建RandomAccessFile将引发异常。 Since you don't have the exception, you have created the RandomAccessFile with A file. 由于没有例外,因此创建了带有A文件的RandomAccessFile。 Make sure it's the correct one. 确保它是正确的。
  • The RandomAccessFile makes use of an internal file position pointer. RandomAccessFile使用内部文件位置指针。 If you're already at the end of the file, and skip some bytes again, it would explain the behavior that you mentioned. 如果您已经在文件末尾,并再次跳过一些字节,它将解释您提到的行为。 Please make sure you are at the correct position in the file. 请确保您在文件中的正确位置。
byte[] line = new byte[200];
r.seek(200 * 99);
r.read(line);
String s = new String(line, "UTF-8"); // Or probably "Cp1252"

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

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