C# 文件数据流内容写入类StreamWriter

程序设计界面

C# 文件数据流内容写入类StreamWriter_第1张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace _6006_B_文件数据流内容写入类StreamWriter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string filePath;
        private void btnWrite_Click(object sender, EventArgs e)
        {
            string msg = "数据写入完成.\n";
            filePath = Application.StartupPath + @"\Mazda2.txt";

            try
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                    {
                        sw.Write(rtxtContent.Text);//将当前控件文本值写入缓冲流
                        sw.Flush();//将缓冲流写入正式的文件基础流

                        long size = sw.BaseStream.Length;

                        msg = msg + "文件位置:" + filePath + "\n";
                        msg = msg + "文件大小:" + size + " bytes \n";
                        MessageBox.Show(msg, "StreamWriter");
                        sw.Close();
                    }
                    fs.Close();
                }
            }

            catch (IOException ex)
            {
                MessageBox.Show("错误消息:" + ex.Message, "IOException异常");
            }
        }

           

        private void btnRead_Click(object sender, EventArgs e)
        {
            string msg = "";
            filePath = Application.StartupPath + @"\Mazda2.txt";

            try
            {
                if (File.Exists(filePath))
                {
                    using (StreamReader sr = new
                        StreamReader(filePath, Encoding.Default))
                    {

                        long size = sr.BaseStream.Length;
                        int countLine = 0;
                        string result = "";

                        //亦可用while(sr.Peek() != -1)来判断
                        while (sr.EndOfStream != true)
                        {
                            result = result + sr.ReadLine() + "\n";
                            countLine = countLine + 1;
                        }

                        msg = msg + "文件位置:" + filePath + "\n";
                        msg = msg + "文件大小:" + size + " bytes \n";
                        msg = msg + "文件行数:" + countLine + " 行";
                        MessageBox.Show(msg, "StreamReader");
                        sr.Close();
                        rtxtContent.Text = result;
                    }
                }
                else
                {
                    MessageBox.Show("[" + filePath + "]不存在.", "读取失败");
                }
            }
            catch (IOException ex)
            {
                MessageBox.Show("错误消息:" + ex.Message, "IOException异常");
            }
        }

        private void btnClear_Click_1(object sender, EventArgs e)
        {
            rtxtContent.Text = "";
        }
    }
}

 

你可能感兴趣的:(C#,后端)