txt文件的读写

1.引用

using System.IO;

2.创建并写

public void WriteTxt(string filepth, string str)
{
  FileStream fs1 = new FileStream(filepth, FileMode.Create, FileAccess.ReadWrite);
  StreamWriter sw = new StreamWriter(fs1);
  sw.WriteLine(str);
  sw.Close();
  fs1.Close();
}

 3.读

public string ReadTxt(string filepath)
{

    FileStream fs1 = new FileStream(filepath, FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader(fs1);

    var line = sr.ReadLine();
    sr.Close();
    fs1.Close();

    return line;
}

 

posted @ 2024-03-02 10:07  荔枝不吃籽  阅读(49)  评论(0)    收藏  举报