zoukankan      html  css  js  c++  java
  • C#文件读取

    找了几个方法

      Stopwatch sw = new Stopwatch();
                sw.Reset();
                sw.Start();
                this.richTextBox1.Text = string.Empty;
                string line = string.Empty;
                StringBuilder sb = new StringBuilder();
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "txt文件|*.txt";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    StreamReader sr = new StreamReader(dlg.FileName, Encoding.GetEncoding("GB2312"));
                    int nchar;
                    nchar = sr.Read();
                    while (nchar != -1)
                    {
                        sb.Append(Convert.ToChar(nchar).ToString());
                        nchar = sr.Read();
    
                    }
                    sr.Close();
                    this.SetRichT(sb.ToString());
                    sw.Stop();
                    MessageBox.Show(sw.ElapsedMilliseconds.ToString());
    
                }

     测试文件174Mb

     耗时:

    方法2

     Stopwatch sw = new Stopwatch();
                sw.Reset();
                sw.Start();
                this.richTextBox1.Text = string.Empty;
                string line = string.Empty;
                StringBuilder sb = new StringBuilder();
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "txt文件|*.txt";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    StreamReader sr = new StreamReader(dlg.FileName, Encoding.GetEncoding("GB2312"));
                    while ((line = sr.ReadLine()) != null)
                    {
                        sb.Append(line);
    
                    }
                    this.SetRichT(sb.ToString()); sw.Stop();
                    MessageBox.Show(sw.ElapsedMilliseconds.ToString());
                }

    耗时:

    方法3

     Stopwatch sw = new Stopwatch();
                sw.Reset();
                sw.Start();
                this.richTextBox1.Text = string.Empty;
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "txt文件|*.txt";
                string line = string.Empty;
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    StreamReader sr = new StreamReader(dlg.FileName, Encoding.GetEncoding("GB2312"));
                    line = sr.ReadToEnd();
                    this.SetRichT(line); sw.Stop();
                    MessageBox.Show(sw.ElapsedMilliseconds.ToString());
                }
     
    delegate void SetRitchText(object o);
            private void SetRichT(object o)
            {
                if (this.richTextBox1.InvokeRequired)
                {
                    this.richTextBox1.BeginInvoke(new SetRitchText(this.SetRichT), o);
                }
                else
                {
    
                    this.richTextBox1.Text += o + "
    ";
                    this.textBox1.Text = o.ToString().Length.ToString();
                }
            }
  • 相关阅读:
    HDU.6681.Rikka with Cake(欧拉公式 树状数组)
    Codeforces.449C.Willem, Chtholly and Seniorious(ODT)
    2017-2018 ACM-ICPC, Asia Daejeon Regional Contest (E,G,H,I,K)
    CF GYM.101987A.Circuits(线段树)
    2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)
    220
    219
    218
    217
    216
  • 原文地址:https://www.cnblogs.com/bantongshui/p/3172644.html
Copyright © 2011-2022 走看看