Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : Read last n lines of a HUGE file

I want to read the last n lines of a very big file without reading the whole file into any buffer/memory area using Java.

I looked around the JDK APIs and Apache Commons I/O and am not able to locate one which is suitable for this purpose.

I was thinking of the way tail or less does it in UNIX. I don't think they load the entire file and then show the last few lines of the file. There should be similar way to do the same in Java too.

like image 333
Gaurav Verma Avatar asked Nov 08 '10 06:11

Gaurav Verma


People also ask

How do you read the last line of a file in Java?

In Java, we can use the Apache Commons IO ReversedLinesFileReader to read the last few lines of a File .

How do you read the next line in a file in Java?

To read the line and move on, we should use the nextLine() method. This method advances the scanner past the current line and returns the input that wasn't reached initially. This method returns the rest of the current line, excluding any line separator at the end of the line.

How is tail command implemented in Java?

Let's get started: java. From main method start executor service to start log file tailer, i.e. crunchifyExecutor. execute(crunchify_tailF); which internally calls run() Also call appendData() method which will add new line to file every 5 seconds.

How do you read the second line of a text file in Java?

//read the file, line by line from txt File file = new File("train/traindata. txt"); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; line = br. readLine(); while(line != null) { lines = line.


2 Answers

I found it the simplest way to do by using ReversedLinesFileReader from apache commons-io api. This method will give you the line from bottom to top of a file and you can specify n_lines value to specify the number of line.

import org.apache.commons.io.input.ReversedLinesFileReader;   File file = new File("D:\\file_name.xml"); int n_lines = 10; int counter = 0;  ReversedLinesFileReader object = new ReversedLinesFileReader(file); while(counter < n_lines) {     System.out.println(object.readLine());     counter++; } 
like image 135
akki_java Avatar answered Sep 28 '22 04:09

akki_java


If you use a RandomAccessFile, you can use length and seek to get to a specific point near the end of the file and then read forward from there.

If you find there weren't enough lines, back up from that point and try again. Once you've figured out where the Nth last line begins, you can seek to there and just read-and-print.

An initial best-guess assumption can be made based on your data properties. For example, if it's a text file, it's possible the line lengths won't exceed an average of 132 so, to get the last five lines, start 660 characters before the end. Then, if you were wrong, try again at 1320 (you can even use what you learned from the last 660 characters to adjust that - example: if those 660 characters were just three lines, the next try could be 660 / 3 * 5, plus maybe a bit extra just in case).

like image 45
paxdiablo Avatar answered Sep 28 '22 05:09

paxdiablo