数据流基础之写文件和读文件

2018-12-09  本文已影响9人  lizg

创建一个窗体应用程序,拖动两个TextBox空间,其中一个具有Multiline=true的属性,拖动两个Button按钮,一个命名为写入文件,一个读取文件

写入文件按钮的代码

       private void btn_write_Click(object sender, EventArgs e)
        {
            string path = @tbx_path.Text.Trim();

            string content = tbx_write.Text;
            FileStream fswriter = new FileStream(path, FileMode.Create, FileAccess.Write);
            byte[] array = Encoding.UTF8.GetBytes(content);
            fswriter.Write(array, 0, array.Length);
            MessageBox.Show("文件写入成功!");
            fswriter.Close();
            fswriter.Dispose();
        }

读取文件按钮的代码

private void btn_read_Click_Click(object sender, EventArgs e)
{
            string path = tbx_path.Text;

            using (FileStream fsReader = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                byte[] array = new byte[1024];
                fsReader.Read(array, 0, (int)fsReader.Length);
                tbx_write.Text = Encoding.UTF8.GetString(array);
            }
     
}
上一篇 下一篇

猜你喜欢

热点阅读