如何使用 StreamReader 读取非常大的文本文件?

发布于 2024-09-02 01:02:07 字数 398 浏览 3 评论 0 原文

我想读取一个巨大的 .txt 文件,但由于其大小过大,导致内存溢出。

有什么帮助吗?

    private void button1_Click(object sender, EventArgs e)
    {
        using (var Reader = new StreamReader(@"C:\Test.txt"))
        {
            textBox1.Text += Reader.ReadLine();
        }
    }

文本文件就是:

Line1
Line2
Line3

字面意思就是这样。

我想将文本文件按原样加载到多行文本框,100% 复制。

I want to read a huge .txt file and I'm getting a memory overflow because of its sheer size.

Any help?

    private void button1_Click(object sender, EventArgs e)
    {
        using (var Reader = new StreamReader(@"C:\Test.txt"))
        {
            textBox1.Text += Reader.ReadLine();
        }
    }

Text file is just:

Line1
Line2
Line3

Literally like that.

I want to load the text file to a multiline textbox just as it is, 100% copy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄风 2024-09-09 01:02:07

首先,您发布的代码只会将文件的第一行放入 TextBox 中。你想要的是这样的:

using (var reader = new StreamReader(@"C:\Test.txt"))
{
    while (!reader.EndOfStream)
        textBox1.Text += reader.ReadLine();
}

现在对于 OutOfMemoryException:我还没有测试过这个,但是你是否尝试过 TextBox.AppendText 方法而不是使用 += ?后者肯定会分配大量字符串,当您接近文件末尾时,其中大多数字符串将接近整个文件的长度。

据我所知,AppendText 也可以这样做;但它的存在让我怀疑它是为了处理这种情况而放在那里的。我可能是错的——就像我说的,还没有亲自测试过。

Firstly, the code you posted will only put the first line of the file into the TextBox. What you want is this:

using (var reader = new StreamReader(@"C:\Test.txt"))
{
    while (!reader.EndOfStream)
        textBox1.Text += reader.ReadLine();
}

Now as for the OutOfMemoryException: I haven't tested this, but have you tried the TextBox.AppendText method instead of using +=? The latter will certainly be allocating a ton of strings, most of which are going to be nearly the length of the entire file by the time you near the end of the file.

For all I know, AppendText does this as well; but its existence leads me to suspect it's put there to deal with this scenario. I could be wrong -- like I said, haven't tested personally.

溺孤伤于心 2024-09-09 01:02:07

通过以下操作,您将获得更快的性能:

textBox1.Text = File.ReadAllText(@"C:\Test.txt");

它还可能有助于解决内存问题,因为在读取每一行时连续分配更大的字符串会浪费大量内存。

诚然,GC 应该在您看到 OutOfMemoryException 之前收集较旧的字符串,但无论如何我都会尝试上述操作。

You'll get much faster performance with the following:

textBox1.Text = File.ReadAllText(@"C:\Test.txt");

It might also help with your memory problem, since you're wasting an enormous amount of memory by allocating successively larger strings with each line read.

Granted, the GC should be collecting the older strings before you see an OutOfMemoryException, but I'd give the above a shot anyway.

橙味迷妹 2024-09-09 01:02:07

首先使用富文本框而不是常规文本框。它们更适合您正在使用的大量数据。但是您仍然需要读取数据。

// use a string builer, the += on that many strings increasing in size
// is causing massive memory hoggage and very well could be part of your problem
StringBuilder sb = new StringBuilder();

// open a stream reader
using (var reader = new StreamReader(@"C:\Test.txt"))
{
    // read through the stream loading up the string builder
    while (!reader.EndOfStream)
    {
       sb.Append( reader.ReadLine() );
    }
}

// set the text and null the string builder for GC
textBox1.Text = sb.ToString();
sb = null;

First use a rich text box instead of a regular text box. They're much better equiped for the large amounts of data you're using. However you still need to read the data in.

// use a string builer, the += on that many strings increasing in size
// is causing massive memory hoggage and very well could be part of your problem
StringBuilder sb = new StringBuilder();

// open a stream reader
using (var reader = new StreamReader(@"C:\Test.txt"))
{
    // read through the stream loading up the string builder
    while (!reader.EndOfStream)
    {
       sb.Append( reader.ReadLine() );
    }
}

// set the text and null the string builder for GC
textBox1.Text = sb.ToString();
sb = null;
肤浅与狂妄 2024-09-09 01:02:07

一次一行地读取并处理它,或者将其分成块并单独处理块。您还可以向我们展示您拥有的代码,并告诉我们您想用它来完成什么任务。

下面是一个示例:C# 读取包含由制表符分隔的数据的文本文件 请注意 ReadLine()WriteLine() 语句。

TextBox 受到它可以容纳的字符数的严重限制。您可以尝试在 AppendText() 方法改为 ="nofollow noreferrer">RichTextBox

Read and process it one line at a time, or break it into chunks and deal with the chunks individually. You can also show us the code you have, and tell us what you are trying to accomplish with it.

Here is an example: C# Read Text File Containing Data Delimited By Tabs Notice the ReadLine() and WriteLine() statements.

TextBox is severely limited by the number of characters it can hold. You can try using the AppendText() method on a RichTextBox instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文