1. 程式人生 > 實用技巧 >五、StreamWriter-基本操作-適合處理文字檔案

五、StreamWriter-基本操作-適合處理文字檔案

1.StreamWriter Write 不換行寫入 存在會覆蓋原檔案

 1         /// <summary>
 2         /// StreamWriter Write方法不換行寫入檔案
 3         /// </summary>
 4         public static void Write()
 5         {
 6             using StreamWriter streamWriter = new StreamWriter("StreamWriterText.txt");
 7 
 8             while
(true) 9 { 10 string message = Console.ReadLine(); 11 12 if (message == "q") 13 break; 14 15 streamWriter.Write(message); 16 } 17 }
View Code

2.StreamWriter WriteLine 換行寫入 存在會覆蓋原檔案

 1         /// <summary>
2 /// 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 }
View Code