By Smart Man


2013-08-07 20:47:14 8 Comments

Hi i have read this question :

Reading very large text files, should I be incorporating async?

I diged the net especially the STACK OVERFLOW !

The results was 14 method to do this but none of them is not complete !

In 2 last days , i am working on this and tested and benchmarked 14 methods.

for example :

        private void method()
        {

        FileStream FS = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);

        int FSBytes = (int) FS.Length;

        int ChunkSize = 24;

        byte[] B = new byte[ChunkSize];

        int Pos;

        for (Pos = 0; Pos < (FSBytes - ChunkSize); Pos += ChunkSize)

        {

        FS.Read(B,0 , ChunkSize);
        string content = System.Text.Encoding.Default.GetString(B);

        richTextBox1.Text=content=;


        }

        B = new byte[FSBytes - Pos];

        FS.Read(B,0, FSBytes - Pos);
        string content2 = System.Text.Encoding.Default.GetString(B);

        richTextBox1Text=content2;


        FS.Close(); 
        FS.Dispose();
        }

for 5mb text file , it takes too long , what should i do ?

1 comments

@Kai Hartmann 2013-08-08 11:30:32

This is a working example of reading a text file per stream to accomplish what you are trying to do. I have tested it with a 100 MB text file, and it worked well, but you have to see if larger files work as well.

This is the example. Just bring a RichTextBox to your form and a VScrollBar. Then use a file 'test.txt' on your hard drive 'C:'.

public partial class Form1 : Form
{
    const int PAGE_SIZE = 64;   // in characters
    int position = 0;  // position in stream

    public Form1()
    {
        InitializeComponent();
    }

    private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
        position = e.NewValue * PAGE_SIZE;

        ReadFile(position);    
    }

    private void ReadFile(int position)
    {
        using (StreamReader sr = new StreamReader(@"C:\test.txt"))
        {
            char[] chars = new char[PAGE_SIZE];
            sr.BaseStream.Seek(position, SeekOrigin.Begin);
            sr.Read(chars, 0, PAGE_SIZE);

            string text = new string(chars);
            richTextBox1.Text = text;
        }    
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ReadFile(position);
    }
}

@Star 2013-08-08 11:48:46

wouldn't richTextBox1.Text = text; replace old value with the new?

@Kai Hartmann 2013-08-08 11:52:08

Yes. When you scroll, the content of the RichTextBox is replaced with the new 'page'.

@Kai Hartmann 2013-08-09 06:24:12

Get the number of characters in the file like this: stackoverflow.com/questions/8214772/… Then divide that number by PAGE_SIZE. You probably will have to round up the result with Math.Ceiling(). So it would be something like: vScrollBar1.Maximum = Math.Ceiling(NumberOfCharacters / PAGE_SIZE);

@Kai Hartmann 2013-08-12 06:24:44

Carriage Return, LineFeed, and the combination of both are the identifiers for ReadLine() to identify the end of a line. msdn.microsoft.com/en-us/library/… So you would have to extend your code, so that it reads characters until these characters. Afterwards you would have to update 'position' accordingly.

@Smart Man 2013-08-12 14:51:49

@ Kai Hartmann : +1 For you. thanks.

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] Calculate text width with JavaScript

  • 2008-09-22 23:37:52
  • Jacob
  • 373038 View
  • 442 Score
  • 23 Answer
  • Tags:   javascript textbox

62 Answered Questions

[SOLVED] How do you split a list into evenly sized chunks?

24 Answered Questions

[SOLVED] Reading settings from app.config or web.config in .NET

44 Answered Questions

[SOLVED] How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

  • 2008-09-29 22:30:28
  • mistrmark
  • 1061171 View
  • 1829 Score
  • 44 Answer
  • Tags:   c# .net excel file-io

35 Answered Questions

[SOLVED] What is the most "pythonic" way to iterate over a list in chunks?

11 Answered Questions

[SOLVED] How do I automatically scroll to the bottom of a multiline text box?

4 Answered Questions

[SOLVED] Read the actual contents of text file using FileStream and these options c#

  • 2013-01-03 07:43:22
  • user1776480
  • 49180 View
  • 20 Score
  • 4 Answer
  • Tags:   c# filestream

2 Answered Questions

[SOLVED] Add image with text in header of every page in pdf file using itextsharp

  • 2016-11-24 13:43:01
  • Sandip Gend
  • 3363 View
  • 1 Score
  • 2 Answer
  • Tags:   c# itext

4 Answered Questions

[SOLVED] C# TCP file transfer - Images semi-transferred

Sponsored Content