static void Main(string[] args)
{
//写入string数组,每个string一行
string[] lines = { "first line", "second line ", "third line", "forth line" };
File.WriteAllLines(@"E:\visual studio 2015 test\IOTest1.txt", lines);
//写入一个字符串
string text = "ahhhhhhhhhhhhhhhhhhhhhhhhhhh";
File.WriteAllText(@"E:\visual studio 2015 test\IOTest2.txt", text);
//StreamWriter类,以流的方式写入
StreamWriter file = new StreamWriter(@"E:\visual studio 2015 test\IOTest3.txt", true);
foreach (var line in lines)
{
if (!line.Contains("second"))
{
file.Write(line);
file.WriteLine(line);
}
}
file.Flush();
file.Close();
}
C#练习---3(字符串写入文本)
最新推荐文章于 2024-07-01 10:35:50 发布