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(文件流)实现对某一文档的 数据的读写  界面自由设计

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值