//创建
FileStream fs = File.Create(@"D:/test.txt");
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("112233445566");
sw.Close();
fs.Close();

//读取
FileStream fss = File.OpenRead(@"D:/test.txt");
StreamReader sr = new StreamReader(fss);
string s = sr.ReadLine();
MessageBox.Show(s);
sr.Close();
fss.Close();

//增加
List<string> list = new List<string>();
list.Add("123456798");
File.AppendAllLines(@"D:/test.txt", list);
File.AppendAllText(@"D:/test.txt", "987654321");
StreamWriter swww= File.AppendText(@"D:/test.txt");
swww.WriteLine();
swww.WriteLine("555555");
swww.Close();

//sw追加

using (StreamWriter fs = new StreamWriter(path, true))
{
fs.WriteLine("");
}

读取 一行一行的读取

using (FileStream fs = new FileStream(@"D:/test.txt", FileMode.Open, FileAccess.Read))
{
StreamReader read = new StreamReader(fs, Encoding.Default);
string strReadline;
while ((strReadline = read.ReadLine()) != null)
{
//strReadline
}
}

posted on 2018-04-26 09:35  撒娇的小熊  阅读(215)  评论(0编辑  收藏  举报