Search code examples
javabufferedreader

How to read a text file in a bottom up fashion in java?


I'm trying to implement a log structure file system as an operating system assignment. In it most recent data is placed at end of file. That's why I want to read text file "line-by-line" in reverse order. Is it possible?


Solution

  • Check out ReverseLineInputStream:

    https://code.google.com/p/lt2-sander-marco/source/browse/src/leertaak2/ReverseLineInputStream.java?spec=svn15&r=15

    It refers to the SO question posted at How to read file from end to start (in reverse order) in Java?

    in = new BufferedReader (new InputStreamReader (new ReverseLineInputStream(file)));
    
    while(true) {
        String line = in.readLine();
        if (line == null) {
            break;
        }
        System.out.println("X:" + line);
    }
    

    (Thanks, @Mark O'Donohue)