Print the last two lines in a text file

I have a text file in which I first want to print the last 6 lines and then determine when a new line has been added so that it continues to refresh the screen using the last steps. The idea is that I am trying to display the last six transactions made in my program.

The problem I'm currently facing is that it continues to print the first (not the last) six lines in a text file when I want it to be the other way around.

Here is my sample code:

  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;
+4
source share
3 answers

String Array. 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();
+2

6 , , . : 5 .txt java

+2

4 , , : (1), 6 (2), .

, , :

barebones, :

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);
        }
    }
}

:

  • , BufferedReader , ,
  • , .
  • , , .

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
+1

Source: https://habr.com/ru/post/1570319/


All Articles