Pages

Sunday, November 18, 2012

How to read a dynamically growing file with Java?

This small post explains, how to read a text file which grows dynamically, using Java. The catalina.out file which displays tomcat's console is a good example for a file which grows dynamically. 

The following example shows, how to read tomcat's catalina.out file with Java. This file grows when the applications deployed on tomcat are being accessed by users. The following program displays newly adding contents of catalina.out file just one second delay and keep continuing it. 

The Java's 'RandomAccessFile' class provides great facilities to read this kind of files.  The major usage of it for the following program is, it provides a feature so that we can read a file from a particular point to onwards. Let's see our small program. 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Timer;
import java.util.TimerTask;


public class ConsoleReader {

   /**
    * @param args
    */
    public static void main(String[] args) {
    File file = new File("/home/semika/apache-tomcat-7.0.25/logs/catalina.out");
       try {
           RandomAccessFile r = new RandomAccessFile(file, "r");
           //First time read
           String str = null;
           while((str = r.readLine()) != null) {
            System.out.println(str);
           }
           r.seek(r.getFilePointer());
           startTimer(r);
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       } 
    }
 
    private static void startTimer(final RandomAccessFile r) {
     Timer timer = new Timer();
     timer.scheduleAtFixedRate(new TimerTask() {
     @Override
     public void run() {
     String str = null;
     try {
          while((str = r.readLine()) != null) {
           System.out.println(str);
          } 
          r.seek(r.getFilePointer()); 
     } catch (IOException e) { 
         e.printStackTrace();
     }
      }
 }, 0, 1000);
    }
}

In the above program, it reads catalina.out file line by line and displays it in console. When you run the program, it will read the whole file up to the end at first and then immediately start a timer to read dynamically adding contents to the file. The timer will always start to read file from a particular point, in above case, it will always start to read the file from the next line which last line was displayed.

While you are accessing your applications deployed in your tomcat container, just keep looking at the output of above program. You can see the tomcat's console just like, you have used to see it before.

3 comments:

  1. import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.util.Timer;
    import java.util.TimerTask;
    public class ConsoleReader {
    public static void main(String[] args) {
    File file = new File("C:/Users/Binayak/Desktop/file1.txt");
    try {
    RandomAccessFile r = new RandomAccessFile(file, "rws");
    //First time read
    String str = null;
    while((str = r.readLine()) != null) {
    System.out.println("read"+str);
    }
    r.seek(r.getFilePointer());
    startTimer(r);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private static void startTimer(final RandomAccessFile r) {
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
    String str = null;
    try {
    while((str = r.readLine()) != null) {
    System.out.println(str);
    }
    r.seek(r.getFilePointer());
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }, 0, 500);
    }
    }

    this is random file access program..if i am adding anything new in that file it is reading that much data..but problem is it is reading after click on save button..i want to read that new data without clicking on save button ..please help..i am trying since from four days

    ReplyDelete
  2. import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.util.Timer;
    import java.util.TimerTask;
    public class ConsoleReader {
    public static void main(String[] args) {
    File file = new File("C:/Users/Binayak/Desktop/file1.txt");
    try {
    RandomAccessFile r = new RandomAccessFile(file, "rws");
    //First time read
    String str = null;
    while((str = r.readLine()) != null) {
    System.out.println("read"+str);
    }
    r.seek(r.getFilePointer());
    startTimer(r);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private static void startTimer(final RandomAccessFile r) {
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
    String str = null;
    try {
    while((str = r.readLine()) != null) {
    System.out.println(str);
    }
    r.seek(r.getFilePointer());
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }, 0, 500);
    }
    }

    this is random file access program..if i am adding anything new in that file it is reading that much data..but problem is it is reading after click on save button..i want to read that new data without clicking on save button ..please help..i am trying since from four days

    ReplyDelete
    Replies
    1. Refer This link :

      https://web.archive.org/web/20160510001134/http://www.informit.com/guides/content.aspx?g=java&seqNum=226

      Delete

Share

Widgets