Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-288

Supply a ReversedLinesFileReader

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.2
    • Utilities
    • 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!

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            henzlerg Georg Henzler
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment