来源:互联网 发布:数据作图软件 编辑:程序博客网 时间:2024/05/01 23:43

private void btnRead_Click(object sender, EventArgs e)

        {

            StreamReader sr = new StreamReader(@"E:/MyFile/my.txt",true);

            string strLine = "";

            strLine = sr.ReadToEnd();

            richTextBox1.Text = strLine;

            sr.Close();

        }

 

        private void btnWrite_Click(object sender, EventArgs e)

        {

            StreamWriter sw = new StreamWriter(@"E:/MyFile/my.txt", true);

            if (richTextBox1.Text != string.Empty)

            {

                sw.WriteLine(richTextBox1.Text);

                MessageBox.Show("文件写入成功");

                this.richTextBox1.Text = "";

            }

            else

            {

                MessageBox.Show("你还没有输入数据");

                return;

            }

            sw.Close();

        }

        /*1.通过写入流(StreamWriter)和读取流(StreamReader)实现对某一文档的 数据的读写 界面自由设计*/

        string strPath = @"E:/MyFile/my.txt";

        private void btnRead_Click(object sender, EventArgs e)

        {

            FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);

            byte[] b = new byte[100];

            fs.Read(b, 0, b.Length);

            string strB = System.Text.Encoding.Default.GetString(b);

            //string strA = string.Empty;

            //while()

            this.richTextBox1.Text = strB;

            fs.Close();

        }

 

        private void btnWrite_Click(object sender, EventArgs e)

        {

            FileStream fs = new FileStream(strPath, FileMode.Create, FileAccess.Write);

            string strWrite = this.richTextBox1.Text;

            byte[] MyByte = System.Text.Encoding.Default.GetBytes(strWrite);

            fs.Write(MyByte, 0, MyByte.Length);

            MessageBox.Show("写入成功");

            this.richTextBox1.Text = "";

            fs.Close();

        }

        //2.    通过FileStream(文件流)实现对某一文档的 数据的读写  界面自由设计

 

原创粉丝点击