控制台读写文件。

namespace ReadLineWriteFile
{
class Program
{
static void Main(string[] args)
{
List<string> listName = new List<string>();
do
{
Console.WriteLine("等待输入(q结束)");
string strname = Console.ReadLine();
if (strname.ToLower() == "q")
{
break;
}
listName.Add(strname);
} while (true);
Read(listName);
Console.WriteLine("OK");
Console.ReadKey();
}
static void Read(List<string> list)
{
for (int i = 0; i < list.Count; i++)
{
Write("test.txt", list[i]);
}
}
static void Write(string path, string str)
{
using (FileStream fs = new FileStream(path, FileMode.Append))
{
using (StreamWriter sr = new StreamWriter(fs,Encoding.Default))
{
sr.WriteLine(str);
}
}
}
}
}

posted @ 2016-12-14 08:56  温柔牛  阅读(308)  评论(0编辑  收藏  举报