c# 文件的读写

时间:2022-06-16 12:23:58

    读文件:

c# 文件的读写c# 文件的读写
 1 StreamReader sr = new StreamReader(FileName, Encoding.Default);
2 string content = "";
3 content = sr.ReadLine();
4
5 while (content != null)
6 {
7 //操作逻辑
8 ....
9 ....
10 content = sr.ReadLine();
11 }
12 sr.Close();
View Code


 

   写文件:

c# 文件的读写c# 文件的读写
 1             string path = @"路径\list_key.txt";
2
3 FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);
4 //创建写入文件
5 StreamWriter sw1 = new StreamWriter(fs1, Encoding.Default);
6 sw1.AutoFlush = true;
7
8 // for (int i = 0; i < strFirstList.Count; i++)
9 // {
10 // sw1.Write(strFirstList[i]);
11 // sw1.Write(" ");
12 // sw1.WriteLine(strSecList[i]);
13 // }
14
15
16 fs1.Close();
View Code