使文本文件的读取更有效(大文件类型)

时间:2012-01-07 19:59:02

标签: c#

以下是我的代码,但一次不能处理超过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 ! ~.~");
        }


    }

4 个答案:

答案 0 :(得分:2)

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

代码中的瓶颈是你要附加字符串。使用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行。您的用户界面中是否存在阻止其处理更多内容的内容?当然,您展示的代码中没有任何内容具有如此低的限制。

答案 1 :(得分:1)

500行没什么。

尝试File.ReadAllLines和File.WriteAllLines。

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

答案 2 :(得分:1)

逐行读取文件非常慢。您可以通过读取大量数据(甚至整个文件,如果它不是太大)来更快地创建此代码。例如,使用File.ReadAllLines将整个文件作为单独的行读取,或者将FileStream和Read()用于缓冲区,并通过查找换行符(\ n,\ r \ n)来找到自己的行。< / p>

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

答案 3 :(得分:0)

使用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 ! ~.~");
        }
    }