Java reading of the nth line

I am trying to read a specific line from a text file, however I do not want to load the file into memory (it can become really big).

I searched, but every example I found requires either reading each line (this will slow down my code, since there will be more than 100,000 lines), or load it all into an array and get the correct element (the file will have many lines for input) .

An example of what I want to do:

String line = File.getLine(5); 

"the code is not actual code, it is designed to show the principle of what I want"

Is there any way to do this?

----- ----- Change

I just realized that this file will also be written between reading lines (appended to the end of the file).

+5
source share
8 answers

Is there any way to do this?

If strings do not have a fixed number of bytes each, no.

You do not need to store each line in memory, but you need to read the entire file to go to the desired line, since otherwise you will not know where to start reading.

+12
source

You should read the file line by line. Otherwise, how do you know when you hit line 5 (as in your example)?

Edit:

You can also check random access files , which can be useful if you know how many bytes per line, as John Skeet said.

+3
source

The easiest way to do this is to use BufferedReader ( http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html ) because you can specify your buffer size. You can do something like:

BufferedReader in = new BufferedReader (new FileReader ("foo.in"), 1024);

 in.readLine(); in.readLine(); in.readLine(); in.readLine(); String line = in.readLine(); 
+3
source

1) read the line that the user selects

If you need to read a line selected by the user only once or several times (or if the file is small enough), you just need to read the file line by line from the very beginning until you reach the selected line.

If, on the other hand, you often need to read a line selected by the user, you must create an index of line numbers and offsets. So, for example, line 42 corresponds to an offset of 2347 bytes per file. Ideally, then you would only read the entire file once and save the index - for example, on a map, using line numbers as keys and offsets as values.

2) read new lines added since last read. I plan to read the file every 10 seconds. I have the number of lines and find out the new line numbers, but I need to read this line

At the second point, you can simply save the current offset to a file instead of saving the current line number, but, of course, it will not hurt to continue building the index if it continues to have a significant performance advantage.

  • Use RandomAccessFile.seek (long offset) to set the file pointer to the last saved offset (confirm that the file is longer than the most recently stored offset, if not, nothing new has been added).
  • Use RandomAccessFile.readLine () to read a line of a file
  • Call RandomAccessFile.getFilePointer () to get the current offset after reading the line and optionally put (currLineNo + 1, offset) in the index.
  • Repeat steps 2-3 until the end of the file is reached.

But don't get too carried away with optimizing performance if performance is not a problem or is likely to be a problem.

+2
source

The only way to do this is to create an index where each line is (you only need to write the end of each line). Without a way to randomly access an index-based string from the beginning, you need to read every byte before that string.

BTW: Reading 100,000 lines can only take one second on a fast machine.

0
source

If performance is very important here, and you often read random lines from a static file, you can optimize it a bit by reading the file and constructing an index (basically just long[] ) of the initial offset of each line of the file.

After that, you know exactly where to go to the file, and then you can read to the next newline to get the full line.

0
source

Here is a piece of code that I had that would read the file and write every 10th line, including the first line, to a new file (writer.) You can always replace the try section with what you want to do. To change the number of lines to read, simply change the value 0 in the if expression "lc.endsWith (" 0 ")" to any line you want to read. But if the file is written when you read it, this code will only work with data contained within the file when this code is run.

  LineNumberReader lnr = new LineNumberReader(new FileReader(new File(file))); lnr.skip(Long.MAX_VALUE); int linecount=lnr.getLineNumber(); lnr.close(); for (int i=0; i<=linecount; i++){ //read lines String line = bufferedReader.readLine(); String lc = String.valueOf(i); if (lc.endsWith("0")){ try{ writer.append(line+"\n"); writer.flush(); }catch(Exception ee){ } } } 
0
source

For small files:

 String line = Files.readAllLines(Paths.get("file.txt")).get(n); 

For large files:

 String line; try (Stream<String> lines = Files.lines(Paths.get("file.txt"))) { line = lines.skip(n).findFirst().get(); } 

Java 7:

 String line; try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) { for (int i = 0; i < n; i++) br.readLine(); line = br.readLine(); } 
0
source

All Articles