Sunday, September 29, 2024

How to Read File From The Last Line in Java

There are applications where you have to read huge files line by line may be using some tool like Pentaho, Camel. Let's say these files are in the format header, records and footer and you need to check something in the footer (last line of the file) and if that condition is not met you need to reject the file. Now, reading line by line in such scenario, will be wasteful as you'll anyway reject the file in the end.

So for such scenarios it is better to read the last line (or may be last N lines) of the file, to have better throughput. In this post you will see how to read a file from the last line in Java.


In Java reading file from the end can be done using RandomAccessFile which has a seek method to set the file-pointer offset, measured from the beginning of the file.

Apache Commons IO also has a ReversedLinesFileReader class which reads lines in a file reversely.

File used

Let's say you have a file aa.txt which has the following content-

This is the first line.
This is the second line.
This is the third line.
This is the fourth line.

And you want to read last line of this file using Java.

Java program to read last line of the file using RandomAccessFile

public class ReadFileLast {
  public static void main(String[] args) {
  ReadFileLast rf = new ReadFileLast();
  File file = new File("F:\\NetJS\\test\\aa.txt");
  // calling method
  rf.readFromLast(file);
  //rf.reverseLines(file);
  }
 
  // Read last line of the file
  public void readFromLast(File file){  
    int lines = 0;
    StringBuilder builder = new StringBuilder();
    RandomAccessFile randomAccessFile = null;
    try {
      randomAccessFile = new RandomAccessFile(file, "r");
      long fileLength = file.length() - 1;
      // Set the pointer at the last of the file
      randomAccessFile.seek(fileLength);
      for(long pointer = fileLength; pointer >= 0; pointer--){
        randomAccessFile.seek(pointer);
        char c;
        // read from the last one char at the time
        c = (char)randomAccessFile.read(); 
        // break when end of the line
        if(c == '\n'){
          break;
        }
        builder.append(c);
      }
      // Since line is read from the last so it 
      // is in reverse so use reverse method to make it right
      builder.reverse();
      System.out.println("Line - " + builder.toString());
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
      if(randomAccessFile != null){
        try {
          randomAccessFile.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
}

Output

Line - This is the fourth line.

Here you get the file length and then using the seek method move the pointer to that point (end of the file). From there you start reading backward char by char. Since you are reading in reverse so you need to use reverse method of the StringBuilder to make the String straight.

Java program to read last n lines of the file

If you want to read last n lines of the file you can use the following method.

// Read n lines from the end of the file
public void readFromLast(File file, int lines){
  int readLines = 0;
  StringBuilder builder = new StringBuilder();
  RandomAccessFile randomAccessFile = null;
  try {
    randomAccessFile = new RandomAccessFile(file, "r");
    long fileLength = file.length() - 1;
    // Set the pointer at the last of the file
    randomAccessFile.seek(fileLength);
    for(long pointer = fileLength; pointer >= 0; pointer--){
      randomAccessFile.seek(pointer);
      char c;
      // read from the last one char at the time
      c = (char)randomAccessFile.read(); 
      // break when end of the line
      if(c == '\n'){
        readLines++;
        if(readLines == lines)
         break;
      }
      builder.append(c);
    }
    // Since line is read from the last so it 
    // is in reverse so use reverse method to make it right
    builder.reverse();
    System.out.println("Line - " + builder.toString());
  } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }finally{
    if(randomAccessFile != null){
      try {
        randomAccessFile.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
}


Read file from the last line using ReversedLinesFileReader

You can also use Apache Commons IO's ReversedLinesFileReader class which reads lines in a file reversely, that way you can read file from the last line.

public class ReadFileLast {

 public static void main(String[] args) {
  ReadFileLast rf = new ReadFileLast();
  File file = new File("F:\\NetJS\\test\\aa.txt");
  // calling method
  rf.reverseLines(file);

 }
 
// Reading file from the last using
// using ReversedLinesFileReader
public void reverseLines(File file){
 ReversedLinesFileReader object = null;
 try {
  object = new ReversedLinesFileReader(file);
  System.out.println("Line - " + object.readLine());
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }finally{
  try {
   object.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}
}

That's all for this topic How to Read File From The Last Line in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Reading Delimited File in Java Using Scanner
  2. How to Append to a File in Java
  3. How to Convert a File to Byte Array
  4. How to Read Properties File in Java
  5. Creating Tar File And GZipping Multiple Files - Java Program

You may also like-

  1. How to Convert Date to String in Java
  2. Lambda Expression Runnable Example
  3. How to Create Deadlock in Java Multi-Threading - Java Program
  4. How to Iterate a HashMap of ArrayLists of String in Java
  5. Multi catch statement in Java 7
  6. Lambda Expressions in Java 8
  7. Fail-Fast Vs Fail-Safe Iterator in Java
  8. Wiring Collections in Spring