五、StreamWriter-基本操作-適合處理文字檔案
阿新 • • 發佈:2021-01-03
1 /// <summary> 2 /// StreamWriter Write方法不換行寫入檔案 3 /// </summary> 4 public static void Write() 5 { 6 using StreamWriter streamWriter = new StreamWriter("StreamWriterText.txt"); 7 8 whileView Code(true) 9 { 10 string message = Console.ReadLine(); 11 12 if (message == "q") 13 break; 14 15 streamWriter.Write(message); 16 } 17 }
1 /// <summary>View Code2 /// StreamWriter WriteLine方法換行寫入檔案 3 /// </summary> 4 public static void WriteLine() 5 { 6 using StreamWriter streamWriter = new StreamWriter("StreamWriterLine.txt"); 7 8 while (true) 9 { 10 string message = Console.ReadLine();11 if (message == "thank you") 12 { 13 Console.WriteLine("退出當前程式"); 14 break; 15 } 16 streamWriter.WriteLine(message); 17 } 18 }