Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#
Article

Separate Into Pages File Reader

Rate me:
Please Sign up or sign in to vote.
4.06/5 (13 votes)
6 Nov 2008CPOL2 min read 25.2K   263   15   2
Separate your files into pages, so you can view your really large files in less than a second!
Image 1

Introduction

This is my first article on The Code Project, so if something's missing please tell me.

This application opens files into pages. The usage of this application is mostly in opening large files (over hundreds of MBs and GBs). It opens these files in a few milliseconds! Other applications (Notepad, Web browsers, etc.) do this job in a few minutes!

Background 

Just a week ago, My friends were trying to open a *.txt file which had their database contents, that file was over 500MB!! They tried to open it in Notepad, but that was not a good choice. Notepad opened that file in over 10 minutes! Other choices were unsuccessful too. Then I asked a question: Is it necessary to open the whole file? The answer was no.
Actually we can't read all characters at once, so it's silly to open all of them. Let's open just a few! 

Using the Code

The idea is very simple. Just open a file, read the first 1000 (for example, it's my default number) characters, close the file and wait for the user to read the text. Then when the user clicks the next page button, open the file again, read the 2nd 1000 characters and so on...

There are two main functions to do this job.
The first and most important function is FillRTB. It opens the input path. Seeks to ((page - 1) * length)th character (because when we're in page 3, 2 pages of characters have been read). Read these characters into a byte array. And load them into my RichTextBox. I had to use MemoryStream because LoadFile only gets streams.
All the Next/Previous/First/Last buttons in my User Control (named Page) use this function to read characters.

C#
void FillRTB(string filepath, long length, int page)
{
    FileInfo fi = new FileInfo(filepath);
    if (page != MaxPage || fi.Length % length == 0)
    {
        byte[] ByteRead = new [length];
        using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
        {
            fs.Seek((page - 1) * length, SeekOrigin.Begin);
            fs.Read(ByteRead, 0, ByteRead.Length);
        }
        SetMaxPage(filepath, length);
        MemoryStream memStream = new MemoryStream(ByteRead);
        FileInfo info = new FileInfo(filepath);

        rtbPage.LoadFile(memStream, RichTextBoxStreamType.PlainText);
    }
    else
    {
        byte[] ByteRead = new byte[fi.Length % length];
        using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
        {
            fs.Seek((page - 1) * length, SeekOrigin.Begin);
            fs.Read(ByteRead, 0, ByteRead.Length);
        }
        SetMaxPage(filepath, length);
        MemoryStream memStream = new MemoryStream(ByteRead);
        FileInfo info = new FileInfo(filepath);

        rtbPage.LoadFile(memStream, RichTextBoxStreamType.PlainText);
    }
}

The else statement is for the last page because the last page may contains less than 1000 (example) characters.

The 2nd function that helps FillRTB to find the number of the last page is SetMaxPage. It also set the current page.

C#
private void SetMaxPage(string filepath, long length)
{
    using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
    {
        if (fs.Length % length == 0)
        {
            lblPage.Text = string.Format("Page {0} - {1}", page, fs.Length / length);
            MaxPage = Convert.ToInt32(fs.Length / length);
        }
        else
        {
            lblPage.Text = string.Format("Page {0} - {1}", page, fs.Length / length + 1);
            MaxPage = Convert.ToInt32(fs.Length / length + 1);
        }
    }
} 

Points of Interest

I hope I can explain well, actually I tried. My application opens files into tabs, the work I've never done before. Tabs are fun, working with them is very interesting.

As you can see, I used the Firefox close button as my close-tab-button. Actually I took pictures from that button and used it, this way. I didn't know (yet) if this button has any copyrights! If it has, please tell me to use another close button!:) 

History

  • 6th November, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaltnx Pin
sajjy8-Nov-08 21:41
sajjy8-Nov-08 21:41 
GeneralSounds like some nice old DOS utilities Pin
supercat97-Nov-08 6:41
supercat97-Nov-08 6:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.