通过滚动条逐块读取文本文件块 [英] read text file chunks by chunks by scrollbar

查看:38
本文介绍了通过滚动条逐块读取文本文件块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了这个问题:

Hi i have read this question :

阅读非常大的文本文件,我应该合并异步?

我挖了网,特别是堆栈溢出!

I diged the net especially the STACK OVERFLOW !

结果是有 14 种方法可以做到这一点,但没有一种方法是不完整的!

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

在过去的两天里,我正在研究这个并测试和基准测试 14 种方法.

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

例如:

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

对于 5mb 的文本文件,它花费的时间太长,我该怎么办?

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

推荐答案

这是一个工作示例,它通过读取每个流的文本文件来完成您正在尝试执行的操作.我已经用 100 MB 的文本文件对其进行了测试,效果很好,但您必须看看更大的文件是否也能正常工作.

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.

这是一个例子.只需将 RichTextBox 带入您的表单和 VScrollBar.然后使用硬盘C:"上的文件test.txt".

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

这篇关于通过滚动条逐块读取文本文件块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆