.NET Reverse Stream Reader

How to read file bottom up:
Read text file bottom up:

When we process the flat files, most of the times we process it top to bottom. However, there might be only a few times situation come where you you have to read bottom up. Like the other day, I have huge flat file about 250 MB and I need some information could be located anywhere in last 50 lines. Since length, size and # of lines I might have to look into in such files vary, reading top to bottom isn’t an optimal solution.

I came across couple of solutions like Use an ArrayList, then iterate backwards. This solution gets the stuff working but makes you read the whole file, store the data in memory and iterate in backwards. Hence it won’t fit my needs as my files are real big and cannot afford the memory to hold 250 MB file contents for reading just 50 lines from the end of file.

So, I started writing my own C#.NET class, Arjarapu.IO.ReverseStreamReader, to read a file from bottom up or read a file in reverse order. ReverseStreamReader extends StreamReader and supports the following methods limited only to ASCII based flat files.

      Read (to buffer)

You may download the source in here. If you have any suggestions to support UNICODE and other encoded files or optimized version of this file, please comment in here.

– Shyam Arjarapu

Keywords: StreamReader, reverse, .NET, Stream Reader, Bottom up, Read a file backwards

Leave a Reply

Your email address will not be published. Required fields are marked *