如何在Java中连续读取文件?

17
我正在尝试弄清楚如何连续读取一个文件,一旦添加了新行,就输出这一行。 我正在使用一个睡眠线程来做到这一点,但它似乎只是在整个文件中全部运行并退出程序。您有什么建议我做错了什么吗?
以下是我的代码:
import java.io.*;
import java.lang.*;
import java.util.*;

class jtail { 
    public static void main (String args[])
            throws InterruptedException, IOException{ 

        BufferedReader br = new BufferedReader(
                new FileReader("\\\\server01\\data\\CommissionPlanLog.txt"));

        String line = null;
        while (br.nextLine ) {
            line = br.readLine();
            if (line == null) {
                //wait until there is more of the file for us to read
                Thread.sleep(1000);
            }
            else {
                System.out.println(line);
            }
        }
    } //end main 
} //end class jtail 

提前感谢您。

更新:我已将代码中的行 "while (br.nextLine ) {" 更改为 "while (TRUE) {"。


这段代码无法编译。in是什么? - Simon Nickerson
只是一条评论,但你应该研究一下使用 Scanner 对象而不是 BufferedReader, 它们更友好,但这不能回答这个问题。 - Pace
仍然存在问题:while (br.nextLine)nextLine不是BufferedReader的有效字段。 - user85421
https://dev59.com/O3RB5IYBdhLWcg3wpoxm - kervin
4个回答

17

这篇文章有点老了,但我使用了这种机制,它的效果相当不错。

编辑:链接已失效,但我在网络归档中找到了它 https://web.archive.org/web/20160510001134/http://www.informit.com/guides/content.aspx?g=java&seqNum=226

关键是使用java.io.RandomAccessFile,并定期检查文件长度是否大于当前文件位置。如果是,则读取数据。当你达到长度时,等待。重复这个过程。

我把代码复制了一份,以防新链接失效。

package com.javasrc.tuning.agent.logfile;

import java.io.*;
import java.util.*;

/**
 * A log file tailer is designed to monitor a log file and send notifications
 * when new lines are added to the log file. This class has a notification
 * strategy similar to a SAX parser: implement the LogFileTailerListener interface,
 * create a LogFileTailer to tail your log file, add yourself as a listener, and
 * start the LogFileTailer. It is your job to interpret the results, build meaningful
 * sets of data, etc. This tailer simply fires notifications containing new log file lines, 
 * one at a time.
 */
public class LogFileTailer extends Thread 
{
  /**
   * How frequently to check for file changes; defaults to 5 seconds
   */
  private long sampleInterval = 5000;

  /**
   * The log file to tail
   */
  private File logfile;

  /**
   * Defines whether the log file tailer should include the entire contents
   * of the exising log file or tail from the end of the file when the tailer starts
   */
  private boolean startAtBeginning = false;

  /**
   * Is the tailer currently tailing?
   */
  private boolean tailing = false;

  /**
   * Set of listeners
   */
  private Set listeners = new HashSet();

  /**
   * Creates a new log file tailer that tails an existing file and checks the file for
   * updates every 5000ms
   */
  public LogFileTailer( File file )
  {
    this.logfile = file;
  }

  /**
   * Creates a new log file tailer
   * 
   * @param file         The file to tail
   * @param sampleInterval    How often to check for updates to the log file (default = 5000ms)
   * @param startAtBeginning   Should the tailer simply tail or should it process the entire
   *               file and continue tailing (true) or simply start tailing from the 
   *               end of the file
   */
  public LogFileTailer( File file, long sampleInterval, boolean startAtBeginning )
  {
    this.logfile = file;
    this.sampleInterval = sampleInterval;
  }

  public void addLogFileTailerListener( LogFileTailerListener l )
  {
    this.listeners.add( l );
  }

  public void removeLogFileTailerListener( LogFileTailerListener l )
  {
    this.listeners.remove( l );
  }

  protected void fireNewLogFileLine( String line )
  {
    for( Iterator i=this.listeners.iterator(); i.hasNext(); )
    {
      LogFileTailerListener l = ( LogFileTailerListener )i.next();
      l.newLogFileLine( line );
    }
  }

  public void stopTailing()
  {
    this.tailing = false;
  }

  public void run()
  {
    // The file pointer keeps track of where we are in the file
    long filePointer = 0;

    // Determine start point
    if( this.startAtBeginning )
    {
      filePointer = 0;
    }
    else
    {
      filePointer = this.logfile.length();
    }

    try
    {
      // Start tailing
      this.tailing = true;
      RandomAccessFile file = new RandomAccessFile( logfile, "r" );
      while( this.tailing )
      {
        try
        {  
          // Compare the length of the file to the file pointer
          long fileLength = this.logfile.length();
          if( fileLength < filePointer ) 
          {
            // Log file must have been rotated or deleted; 
            // reopen the file and reset the file pointer
            file = new RandomAccessFile( logfile, "r" );
            filePointer = 0;
          }

          if( fileLength > filePointer ) 
          {
            // There is data to read
            file.seek( filePointer );
            String line = file.readLine();
            while( line != null )
            {
              this.fireNewLogFileLine( line );
              line = file.readLine();
            }
            filePointer = file.getFilePointer();
          }

          // Sleep for the specified interval
          sleep( this.sampleInterval );
        }
        catch( Exception e )
        {
        }
      }

      // Close the file that we are tailing
      file.close();
    }
    catch( Exception e )
    {
      e.printStackTrace();
    }
  }
}

6
如果您计划在一个相当大的应用程序中实现此功能,其中多个对象可能对处理文件中的新行感兴趣,那么您可能需要考虑使用观察者模式。
从文件读取的对象将立即通知每个订阅它的对象一旦一行已被处理。这将使您能够在需要的类中保持逻辑分离。

4

2

目前你的代码写法存在问题,当'line==null'时,你在进入循环之前就已经检查过是否有下一行了。因此不会进入while循环。

建议改用while(true){ }循环方式,这样你就可以一直循环执行,同时通过判断条件来解决程序结束问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接