Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.2
    • Component/s: Utilities
    • Labels:
      None

      Description

      I needed to analyse a log file today and I was looking for a ReversedLinesFileReader: A class that behaves exactly like BufferedReader except that it goes from bottom to top when readLine() is called. I didn't find it in IOUtils and the internet didn't help a lot either, e.g. http://www.java2s.com/Tutorial/Java/0180__File/ReversingaFile.htm is a fairly inefficient - the log files I'm analysing are huge and it is not a good idea to load the whole content in the memory.

      So I ended up writing an implementation myself using little memory and the class RandomAccessFile - see attached file. It's used as follows:

      int blockSize = 4096; // only that much memory is needed, no matter how big the file is
      ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader (myFile, blockSize, "UTF-8"); // encoding is supported
      String line = null;
      while((line=reversedLinesFileReader.readLine())!=null) {
      ... // use the line
      if(enoughLinesSeen)

      { break; }

      }
      reversedLinesFileReader.close();

      I believe this could be useful for other people as well!

        Activity

        Gary Gregory made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Sebb made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Georg Henzler made changes -
        Attachment ReversedLinesFileReader0.2.zip [ 12503689 ]
        Georg Henzler made changes -
        Attachment ReversedLinesFileReader0.3.zip [ 12503820 ]
        Georg Henzler made changes -
        Attachment ReversedLinesFileReader0.2.zip [ 12503689 ]
        Georg Henzler made changes -
        Attachment ReversedLinesFileReader.zip [ 12503352 ]
        Georg Henzler made changes -
        Attachment ReversedLinesFileReader.zip [ 12503352 ]
        Georg Henzler made changes -
        Attachment ReversedLinesFileReader.zip [ 12503188 ]
        Henri Yandell made changes -
        Fix Version/s 2.2 [ 12318448 ]
        Georg Henzler made changes -
        Attachment ReversedLinesFileReader.zip [ 12503188 ]
        Georg Henzler made changes -
        Attachment ReversedLinesFileReader.zip [ 12502480 ]
        Georg Henzler made changes -
        Summary Supply a ReverseFileReader Supply a ReversedLinesFileReader
        Georg Henzler made changes -
        Description I needed to analyse a log file today and I was looking for a ReverseFileReader: A class that behaves exactly like BufferedReader except that it goes from bottom to top when readLine() is called. I didn't find it in IOUtils and the internet didn't help a lot either, e.g. http://www.java2s.com/Tutorial/Java/0180__File/ReversingaFile.htm is a fairly inefficient - the log files I'm analysing are huge and it is not a good idea to load the whole content in the memory.

        So I ended up writing an implementation myself using little memory and the class RandomAccessFile - see attached file. It's used as follows:

        int blockSize = 4096; // only that much memory is needed, no matter how big the file is
        ReverseFileReader reverseFileReader = new ReverseFileReader(myFile, blockSize, "UTF-8"); // encoding is supported
        String line = null;
        while((line=reverseFileReader.readLine())!=null) {
          ... // use the line
          if(enoughLinesSeen) {
             break;
          }

        }
        reverseFileReader.close();

        I believe this could be useful for other people as well!
        I needed to analyse a log file today and I was looking for a ReversedLinesFileReader: A class that behaves exactly like BufferedReader except that it goes from bottom to top when readLine() is called. I didn't find it in IOUtils and the internet didn't help a lot either, e.g. http://www.java2s.com/Tutorial/Java/0180__File/ReversingaFile.htm is a fairly inefficient - the log files I'm analysing are huge and it is not a good idea to load the whole content in the memory.

        So I ended up writing an implementation myself using little memory and the class RandomAccessFile - see attached file. It's used as follows:

        int blockSize = 4096; // only that much memory is needed, no matter how big the file is
        ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader (myFile, blockSize, "UTF-8"); // encoding is supported
        String line = null;
        while((line=reversedLinesFileReader.readLine())!=null) {
          ... // use the line
          if(enoughLinesSeen) {
             break;
          }

        }
        reversedLinesFileReader.close();

        I believe this could be useful for other people as well!
        Georg Henzler made changes -
        Attachment ReversedLinesFileReader.zip [ 12502480 ]
        Georg Henzler made changes -
        Attachment ReverseFileReader.java [ 12499607 ]
        Georg Henzler made changes -
        Attachment ReverseFileReader.java [ 12499607 ]
        Georg Henzler made changes -
        Attachment ReverseFileReader.java [ 12499594 ]
        Georg Henzler made changes -
        Description I needed to analyse a log file today and I was looking for a ReverseFileReader: A class that behaves exactly like BufferedReader except that it goes from bottom to top when readLine() is called. I didn't find it in IOUtils and the internet didn't help a lot either, e.g. http://www.java2s.com/Tutorial/Java/0180__File/ReversingaFile.htm is a fairly inefficient - the log files I'm analysing is huge and it is not a good idea to load the whole content in the memory.

        So I ended up writing an implementation myself using little memory and the class RandomAccessFile - see attached file. It's used follows:

        int blockSize = 4096; // only that much memory is needed, no matter how big the file is
        ReverseFileReader reverseFileReader = new ReverseFileReader(myFile, blockSize, "UTF-8");
        String line = null;
        while((line=reverseFileReader.readLine())!=null) {
          ... // use the line
          if(enoughLinesSeen) {
             break;
          }

        }
        reverseFileReader.close();

        I believe this could be useful for other people as well!
        I needed to analyse a log file today and I was looking for a ReverseFileReader: A class that behaves exactly like BufferedReader except that it goes from bottom to top when readLine() is called. I didn't find it in IOUtils and the internet didn't help a lot either, e.g. http://www.java2s.com/Tutorial/Java/0180__File/ReversingaFile.htm is a fairly inefficient - the log files I'm analysing are huge and it is not a good idea to load the whole content in the memory.

        So I ended up writing an implementation myself using little memory and the class RandomAccessFile - see attached file. It's used as follows:

        int blockSize = 4096; // only that much memory is needed, no matter how big the file is
        ReverseFileReader reverseFileReader = new ReverseFileReader(myFile, blockSize, "UTF-8"); // encoding is supported
        String line = null;
        while((line=reverseFileReader.readLine())!=null) {
          ... // use the line
          if(enoughLinesSeen) {
             break;
          }

        }
        reverseFileReader.close();

        I believe this could be useful for other people as well!
        Georg Henzler made changes -
        Field Original Value New Value
        Attachment ReverseFileReader.java [ 12499594 ]
        Georg Henzler created issue -

          People

          • Assignee:
            Unassigned
            Reporter:
            Georg Henzler
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development