namespace FileApplication { class Program { static void Main(string[] args) {
string[] names = new string[] {"Zara Ali", "Nuha Ali"}; using (StreamWriter sw = new StreamWriter("names.txt")) { foreach (string s in names) { sw.WriteLine(s);
} }
// 从文件中读取并显示每行 string line = ""; using (StreamReader sr = new StreamReader("names.txt")) { while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } Console.ReadKey(); } } }
如果乱码,可以以下解决:
new StreamReader("C:/a.txt", Encoding.GetEncoding("GB2312"))