-8

こんにちは、私はこの質問を読みました:

非常に大きなテキスト ファイルを読み取る場合、非同期を組み込む必要がありますか?

ネット、特に STACK OVERFLOW を掘りました !

結果はこれを行うための 14 の方法でしたが、どれも完全ではありませんでした。

最後の 2 日間で、私はこれに取り組んでおり、14 の方法をテストしてベンチマークしました。

例えば ​​:

        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();
        }

5 MB のテキスト ファイルの場合、時間がかかりすぎます。どうすればよいですか?

4

1 に答える 1

1

これは、ストリームごとにテキスト ファイルを読み取って目的を達成する実際の例です。100 MB のテキスト ファイルでテストしたところ、問題なく動作しましたが、より大きなファイルでも動作するかどうかを確認する必要があります。

これがその例です。RichTextBox をフォームと VScrollBar に持ってくるだけです。次に、ハード ドライブ「C:」にある「test.txt」ファイルを使用します。

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);
    }
}
于 2013-08-08T11:30:32.803 に答える