使文本文件的阅读更高效(文件类型更大)

本文关键字:文件 类型 高效 文本 | 更新日期: 2023-09-27 18:19:39

以下是我的代码,但它不能一次处理500行以上。

它需要在行的末尾添加一个,同时进行检测。我目前正在做的是将它们分成两个不同的文本框,然后通过复制粘贴保存我需要的文本框。但如果文件太大,应用程序似乎会挂起。

有人能帮我提高效率吗。非常感谢。

 private void button1_Click_1(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
            return;
        System.IO.StreamReader Reader = new System.IO.StreamReader(openFileDialog1.FileName);
        //Create a filestream
        FileStream fStr;
        try
        {
            //Set filestream to the result of the pick of the user
            fStr = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);

            //Create a streamreader, sr, to read the file
            StreamReader sr = new StreamReader(fStr);
            //While the end of the file has not been reached...
            while (sr.Peek() >= 0)
            {
                //Create a 'line' that contains the current line of the textfile
                string line = sr.ReadLine().ToLower();
                if (line.Contains("staff"))
                {
                    line += ","; //Add a , to the end of the line**Important**
                    textBox1.Text += line + Environment.NewLine;
                    releventcount += 1;
                }
                else
                {
                    line += ","; //Add a , to the end of the line**Important**
                    textBox2.Text += line + Environment.NewLine;
                    irreleventcount += 1;
                }
                label1.Text = "Relevent: ";
                label2.Text = "Irrelevant: ";
            }
            //Close the file so other modules can access it
            sr.Close();
            //If something goes wrong, tell the user
        }
        catch (Exception)
        {
            MessageBox.Show("Error opening file", "Check the CODE ! ~.~");
        }

    }

使文本文件的阅读更高效(文件类型更大)

我不确定你最终要在这里完成什么。有几种更简洁的方法可以做当前代码正在做的事情,但它们不会显著提高阅读速度。

代码中的瓶颈是附加字符串。使用StringBuilder是一个很好的建议,但您可以通过创建List<string>然后在最后调用string.Join来做得更好。例如:

if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
    return;
List<string> staff = new List<string>();
List<string> other = new List<string>();
foreach (var line in File.ReadLines(openFileDialog1.FileName))
{
    line = line.ToLower();
    if (line.Contains("staff"))
    {
        staff.Add(line);
    }
    else
    {
        other.Add(line);
    }
}
relevantcount = staff.Count;
irrelevantCount = other.Count;
textBox1.Text = string.Join(","+Environment.NewLine, staff);
textBox2.Text = string.Join("."+Environment.NewLine, other);

另外,您说您的代码一次只能处理500行。您的用户界面中是否存在阻止其处理更多内容的内容?当然,您展示的代码中没有任何内容具有如此低的限制。

500行算不了什么。

尝试File.ReadAllLines和File.WriteAllLines.

然后,您可以对内存中的字符串数组进行处理,并避免迭代IO。

逐行读取文件的速度非常慢。通过读取一大块数据(如果不是太大的话,甚至可以读取整个文件),可以使代码变得更快。例如,使用File.ReadAllLines将整个文件作为单独的行读取,或者使用FileStream和read()将其放入缓冲区,并通过查找换行符(''n,''r)为自己查找单独的行。

要导出数据,不要将其复制并粘贴到文本框中——将结果写入一两个新文件,然后打开它们。

使用StringBuilders为文本框收集文本比连续附加文本效率高得多。

此外,您应该使用块来包装您的各种流。

这里有一个应该更高效的重写:

    private void button1_Click_1(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
            return;
        try
        {
            //Set filestream to the result of the pick of the user
            using (var fStr = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read))
            {
                //Create a streamreader, sr, to read the file
                using (var sr = new StreamReader(fStr))
                {
                    var sbTextBox1 = new System.Text.StringBuilder(10000);
                    var sbTextBox2 = new System.Text.StringBuilder(10000);
                    //While the end of the file has not been reached...
                    while (sr.Peek() >= 0)
                    {
                        //Create a 'line' that contains the current line of the textfile
                        string line = sr.ReadLine().ToLower();
                        if (line.Contains("staff"))
                        {
                            //Add a , to the end of the line**Important**
                            sbTextBox1.Append(line).Append(",").AppendLine();
                            releventcount += 1;
                        }
                        else
                        {
                            //Add a , to the end of the line**Important**
                            sbTextBox2.Append(line).Append(",").AppendLine();
                            irreleventcount += 1;
                        }
                    }
                    textBox1.Text = sbTextBox1.ToString();
                    textBox2.Text = sbTextBox2.ToString();
                    label1.Text = "Relevent: ";
                    label2.Text = "Irrelevant: ";
                    //Close the file so other modules can access it
                    sr.Close();
                }
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Error opening file", "Check the CODE ! ~.~");
        }
    }