Java IO implementation of unix/linux “tail -f”

前端 未结 9 1700
我寻月下人不归
我寻月下人不归 2020-11-22 12:06

I\'m wondering what techniques and/or library to use to implement the functionality of the linux command \"tail -f \". I\'m essentially looking for a drop in add-on/replace

相关标签:
9条回答
  • 2020-11-22 12:49

    If your code only ever will have to run on Unix systems, you may be able to get away with just shelling out and calling tail -f directly.

    As a more involved alternative, you could take a look at the implementation of GNU tail and port that over to Java. (I'm not sure whether this wouldn't already make your code a derivative work, though.)

    0 讨论(0)
  • 2020-11-22 12:51

    I found this nice tail implementation.

    Author : amelandri

    Souce from : https://gist.github.com/amelandri/1376896

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    /**
     * Java implementation of the Unix tail command
     * 
     * @param args[0] File name
     * @param args[1] Update time (seconds). Optional. Default value is 1 second
     * 
     * @author Luigi Viggiano (original author) http://it.newinstance.it/2005/11/19/listening-changes-on-a-text-file-unix-tail-implementation-with-java/
     * @author Alessandro Melandri (modified by)
     * */
    public class Tail {
    
      static long sleepTime = 1000;
    
      public static void main(String[] args) throws IOException {
    
        if (args.length > 0){
    
          if (args.length > 1)
            sleepTime = Long.parseLong(args[1]) * 1000;
    
          BufferedReader input = new BufferedReader(new FileReader(args[0]));
          String currentLine = null;
    
          while (true) {
    
            if ((currentLine = input.readLine()) != null) {
              System.out.println(currentLine);
              continue;
            }
    
            try {
              Thread.sleep(sleepTime);
            } catch (InterruptedException e) {
              Thread.currentThread().interrupt();
              break;
            }
    
          }
          input.close();
    
        } else {
          System.out.println("Missing parameter!\nUsage: java JavaTail fileName [updateTime (Seconds. default to 1 second)]");
            }
          }
    
    }
    
    0 讨论(0)
  • 2020-11-22 12:56

    Check JLogTailer, which does this logic.

    The main point in the code is:

    public void run() {
        try {
            while (_running) {
                Thread.sleep(_updateInterval);
                long len = _file.length();
                if (len < _filePointer) {
                    // Log must have been jibbled or deleted.
                    this.appendMessage("Log file was reset. Restarting logging from start of file.");
                    _filePointer = len;
                }
                else if (len > _filePointer) {
                    // File must have had something added to it!
                    RandomAccessFile raf = new RandomAccessFile(_file, "r");
                    raf.seek(_filePointer);
                    String line = null;
                    while ((line = raf.readLine()) != null) {
                        this.appendLine(line);
                    }
                    _filePointer = raf.getFilePointer();
                    raf.close();
                }
            }
        }
        catch (Exception e) {
            this.appendMessage("Fatal error reading log file, log tailing has stopped.");
        }
        // dispose();
    }
    
    0 讨论(0)
提交回复
热议问题