commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Georg Henzler (Updated) (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (IO-288) Supply a ReversedLinesFileReader
Date Mon, 14 Nov 2011 23:25:51 GMT

     [ https://issues.apache.org/jira/browse/IO-288?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Georg Henzler updated IO-288:
-----------------------------

    Attachment: ReversedLinesFileReader0.2.zip

I didn't really like the "inefficient" idea of using the BufferedReader to get around the
encoding issue... so I read up about encodings in general and I think the solution as provided
works for all one byte encodings, UTF-8, UTF-16BE/UTF-16LE and Shift-JIS. For other encodings
at the moment an exception is thrown to be on the safe side (but this can be easily extended).

Also, now \r alone is treated as newline as well.

PS: Added Apache headers and removed author tag.
                
> Supply a ReversedLinesFileReader 
> ---------------------------------
>
>                 Key: IO-288
>                 URL: https://issues.apache.org/jira/browse/IO-288
>             Project: Commons IO
>          Issue Type: New Feature
>          Components: Utilities
>            Reporter: Georg Henzler
>             Fix For: 2.2
>
>         Attachments: ReversedLinesFileReader0.2.zip
>
>
> 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!

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message