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();
                }
            }
  • 相关阅读:
    pandas之中文分词,词云,情感分析,语义分析3
    pandas之中文分词,词云,情感分析,语义分析2
    pandas之中文分词,词云,情感分析,语义分析1
    大数据学习之-the king of bigdata
    pandas之分组聚合(agg,apply)
    pandas之表的长宽转换
    Pandas数据规整之合并
    Pandas之数据规整
    Pandas数据规整
    IP协议那些事
  • 原文地址:https://www.cnblogs.com/bantongshui/p/3172644.html
Copyright © 2011-2022 走看看