简体   繁体   English

如何在 Java 中返回随机访问文件中一行的位置?

[英]How can i return the position of a line in a Random Access file in Java?

I create a random access file and I write some info in it.我创建了一个随机访问文件,并在其中写入了一些信息。 I would like to store in an array the position of each line in the random access file.我想将随机访问文件中每一行的位置存储在一个数组中。 I'm going to have an index which points to every line of the random access file.我将有一个指向随机访问文件每一行的索引。 So I need to store in my index the positions of the lines the random access file.所以我需要在我的索引中存储随机访问文件的行的位置。

My program is as follows我的程序如下

package randomaccessfile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class raf {

   public static void main(String[] args) throws FileNotFoundException, IOException {
      File file = new File("DocumentsFile.txt");
      RandomAccessFile raf = new RandomAccessFile(file, "rw");
      for (int i = 0; i <= 10 ; i++) {
         String sentence = "1 Info Name Surname"; //i create sentences in the random access file
         raf.seek(file.length());
         raf.writeBytes(sentence);
         raf.writeBytes("\r\n");
      }
      raf.close();
   }
}

For each of the lines that are created in the Random access file, I would like to store in an array their positions.对于在随机访问文件中创建的每一行,我想将它们的位置存储在一个数组中。

The positions then are going to be stored in the index.然后这些位置将被存储在索引中。

Is there any method that I can use in order to return the position of a line in the random access file?有没有什么方法可以用来返回随机访问文件中一行的位置?

RandomAccessFile already provides everything you want. RandomAccessFile已经提供了您想要的一切。

For one, you don't need seek() here: the file pointer advances past the bytes written into the file on each write operation. 首先,您不需要在这里seek() :文件指针会越过每次写操作写入文件的字节。

This means that, after you have written line n , for whatever value of n , grabbing the result of .getFilePointer() from the RandomAccessFile object will give you the starting offset of line n+1 . 这意味着,在编写了第n行之后,无论n值是多少,从RandomAccessFile对象中获取.getFilePointer()的结果都将为您提供第n+1行的起始偏移量。 Line 1 starts at offset 0 and that's pretty much the only thing you have to really account for here. 第1行从偏移量0开始,这几乎是您唯一真正需要考虑的地方。

You can use a temporary variable to store the last FilePointer.您可以使用临时变量来存储最后一个 FilePointer。 If the readline must be reversed, you can set the reader pointer using this variable.如果必须反转读取行,您可以使用此变量设置读取器指针。

RandomAccessFile myReader = new RandomAccessFile(fileObject, "r");    
long current_postion = myReader.getFilePointer();
myReader.readLine();
if (line should be reversed){
   myReader.seek(current_postion); //Point to the previous line
}

On the other hand, if what you really care about are line numbers, you could use a LineNumberReader to map every line in the written file to a given line number. 另一方面,如果您真正关心的是行号,则可以使用LineNumberReader将写入文件中的每一行映射到给定的行号。

File file = new File("jedi.txt");

try(LineNumberReader lnr = new LineNumberReader(new FileReader(file))){
    String line = null;

    while ( (line = lnr.readLine()) != null){
        System.out.println(lnr.getLineNumber() + " " + line );
    }
}catch(IOException e){
    e.printStackTrace();
}

Of course this has nothing to do with RandomAccessFiles, but it could be an alternative depending on what you are doing. 当然,这与RandomAccessFiles无关,但是可以根据您的操作进行选择。

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

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