写入
- 整体写入
public static void WriteFile(string path,string Mes)
{
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(Mes);
sw.Close();
fs.Close();
}
- 一行行写入数据
public static void WriteByLine(string path,string DataLine)
{
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(DataLine);
sw.Close();
fs.Close();
}
读取
- 整体读取
public static string ReadFile(string path)
{
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
string Mes = sr.ReadToEnd();
sr.Close();
fs.Close();
return Mes;
}
- 一行行读取
public static List<string> ReadByLine(string path)
{
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
List<string> Mes = new List<string>();
string line;
while ((line = sr.ReadLine()) != null)
{
Mes.Add(line);
}
sr.Close();
fs.Close();
return Mes;
}