Click here to Skip to main content
Click here to Skip to main content

Separate Into Pages File Reader

, 6 Nov 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Separate your files into pages, so you can view your really large files in less than a second!

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.

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.

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!Smile | :)  

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)

Share

About the Author

Pedram Behroozi

Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

 
Generaltnx Pinmembersajjy8-Nov-08 22:41 
GeneralSounds like some nice old DOS utilities Pinmembersupercat97-Nov-08 7:41 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 6 Nov 2008
Article Copyright 2008 by Pedram Behroozi
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid