2015-01-07 27 views
4

我有一个文本文件,我首先要打印最后6行,然后检测何时添加了新行,以便它会随着最近的活动更新屏幕。我的想法是,我试图显示我的程序中最近做出的六笔交易。在文本文件中打印最后几行

我目前遇到的问题是,它会一直打印文本文件中的第一行(不是最后一行),当我希望它是另一种方式时。

这里是我的示例代码:

BufferedReader in = new BufferedReader(new FileReader("transaction-list.txt")); 

    System.out.println(); 
    System.out.println("SIX MOST RECENT TRANSACTIONS:"); 
    System.out.println(); 

    String line; 

    for (int i=0; i<6;i++){ 
    line=in.readLine(); 
    System.out.println(line); 
    } 

    in.close(); 

}catch (IOException e){ 
    e.printStackTrace(); 
} 
break; 
+2

[爪哇IO执行的Unix/Linux“尾-f的](http://stackoverflow.com/questions/557844/java-io-implementation -of-unix-linux-tail -f) – Reimeus

+0

我不确定为什么这已经被重复关闭,问题说明与链接问题不一样(它是读取文件然后监视新行),而是首先显示最近的6行(OP无法工作),然后继续监视。 –

回答

2

您必须将行保存到字符串数组。并在读完整个文件后才打印Array。只记得从哪里开始保存阵列的读..

BufferedReader in = new BufferedReader(new FileReader("transaction-list.txt")); 
    System.out.println(); 
    System.out.println("SIX MOST RECENT TRANSACTIONS:"); 
    System.out.println(); 
    String[] last6 = new String[6]; 
    int count=0; 
    while(in.ready()){ 
     last6[count++%6]=in.readLine(); 
    } 
    for (int i=0; i<6;i++){ 
     System.out.println(last6[(i+count)%6]); 
    } 
    in.close(); 
+0

作品欢呼只是想知道如果没有行可读,你将如何停止空打印? – simon

+0

我只是祈求上帝...... :-p,添加catch块.. – gifpif

+0

为什么在ArrayDeque已经存在的时候使用数组构建自己的环缓冲区?此外,您并未监视新线路的添加。 –

1

虽然还有其他4个答案,我不认为任何地址都贵点:(1)打印最后6号线和(2) 然后继续监视文件并打印新行

我也认为你应该保持它的简单,以更好地传达你的代码的意图,并删除错误的风险:

  1. 只使用一个BufferedReader而非RandomAccessFile - 这就是BufferedReader
  2. ,而不是使用阵列只使用一个FIFO队列就像ArrayDeque<String> - 这是一个完美的使用情况,并在“ringbuffer”实现完全封装内ArrayDeque

准系统实现其做这一切会是这样的:

public static void MonitorFile(String filePath) 
    throws FileNotFoundException, IOException, InterruptedException 
{ 
    // Used for demo only: count lines after init to exit function after n new lines 
    int newLineCount = 0; 

    // constants 
    final int INITIAL_LINE_LIMIT = 6; 
    final int POLLING_INTERVAL = 1000; 

    // file readers 
    FileReader file = new FileReader(filePath); 
    BufferedReader fr = new BufferedReader(file); 

    // read-and-monitor loop 
    boolean initialising = true; 
    Queue<String> lineBuffer = new ArrayDeque<String>(INITIAL_LINE_LIMIT); 
    int lineCount = 0; 
    while (true) { 
     String line= fr.readLine(); 
     if (line != null) 
     { 
      if (initialising) { // buffer 
       lineBuffer.add(line); 
       if (++lineCount > INITIAL_LINE_LIMIT) lineBuffer.remove(); 
      } 
      else { // print 
       System.out.printf("%d %s%n", ++lineCount, line); 
       newLineCount++; 
      } 
     } 
     else 
     { 
      // No more lines, so dump buffer and/or start monitoring 
      if (initialising) 
      { 
       initialising = false; 
       // reset the line numbers for printing 
       lineCount = Math.max(0, lineCount - INITIAL_LINE_LIMIT); 
       // print out the buffered lines 
       while((line = lineBuffer.poll()) != null) 
        System.out.printf("%d %s%n", ++lineCount, line); 

       System.out.println("finished pre-loading file: now monitoring changes"); 
      } 
      // Wait and try and read again. 
      if (newLineCount > 2) break; // demo only: terminate after 2 new lines 
      else Thread.sleep(POLLING_INTERVAL); 
     } 
    } 
} 

考虑的要点:

  1. 对于它的价值,我会通过BufferedReader作为一个参数所以这变得更加普遍,
  2. 这需要某种取消,所以它不会永远监视。
  3. 您可以使用file change monitoring,而不是轮询和睡眠您的线程,但该代码将比适合此答案更复杂。

上面的代码给出以下输出

2 test line b 
3 test line c 
4 test line d 
5 test line e 
6 test line f 
7 test line g 
finished pre-loading file: now monitoring changes 
8 test line h 
9 test line i 
10 test line j 
11 test line k 
相关问题