C# 将内容写入txt文档

时间:2022-08-12 05:42:24

<1>  FileStream fs = new FileStream(@"D:\text.txt", FileMode.Append);

        StreamWriter sw = new StreamWriter(fs, Encoding.Default);

        sw.Write(strAnalasy);

        sw.Close();

        fs.Close();

<2>  FileStream TreatProcess = new FileStream(@"文件目录", FileMode.Append);

        TreatProcess.Write(receiveData, i + 2, 4);

        TreatProcess.Close();

    稳定的写入txt文件的方式:

        FileStream fs = new FileStream("zhanglei.txt", FileMode.Create);

        byte[] data = System.Text.Encoding.Default.GetBytes("a" + System.Environment.NewLine +"b");

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

        fs.Flush();

        fs.Close();

    稳定的读txt文档的方式:

       StreamReader sr = new StreamReader("zhanglei.txt", Encoding.Default);

       string line = null;

       while ((line = sr.ReadLine()) != null)

       {

             MessageBox.Show(line);

       }

       sr.Close();