zoukankan      html  css  js  c++  java
  • OpenRead方法打开文件并读取

    实现效果:

      

    知识运用
      File类的OpenRead方法    //实现打开现有文件以进行读取

      public static FileStream OpenRead(string path)

      FileStream类的Read方法  //实现从流中读取字节块并将该数据写入给定的缓冲区

      public overrider int Read (byte[] array, int offset, int count)

      补充:Encoding类的GetStrin方法  //实现将字节数组解码为对应的字符串

    实现代码:

            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    OpenFileDialog OFDialog = new OpenFileDialog();
                    OFDialog.Filter = "文本文件(*.txt)|*.txt";
                    OFDialog.ShowDialog();
                    textBox1.Text = OFDialog.FileName;
                    FileStream fs = File.OpenRead(textBox1.Text);
                    byte[] bt=new byte[1024];
                    while(fs.Read(bt,0,bt.Length)>0)
                    {
                        textBox2.Text = Encoding.UTF8.GetString(bt);
                    }
                }
                catch (Exception)
                { MessageBox.Show("请选择文件"); }
            }
    

      

  • 相关阅读:
    BZOJ1087=Codevs2451=洛谷P1896&P2326互不侵犯
    poj1286
    P1066 2^k进制数
    开车旅行
    洛谷P1396 营救
    poj1840
    poj3693
    poj1195
    3955 最长严格上升子序列(加强版)
    1021 玛丽卡
  • 原文地址:https://www.cnblogs.com/feiyucha/p/10252829.html
Copyright © 2011-2022 走看看