[問題] 關於.txt檔輸入與輸出
小弟使用StreamReader和StreamWriter來進行讀和寫
可是這兩個不能直接指定要在.txt檔的哪一行進行讀寫
以StreamWriter來說,我看MSDN上的範例可以讓它寫完第一行
再接著寫第二行,可是我不想把第一行給覆蓋掉
請問該怎麼辦???
舉例來說,StreamWriter是第一行輸入完後在換下一行如下
StreamWriter sw = new StreamWriter("C:\\Test.txt");
//Write a line of text
sw.WriteLine("Hello World!!");
//Write a second line of text
sw.WriteLine("From the StreamWriter class");
//Close the file
sw.Close();
但是現在如果Test.txt檔裡面原本第一行就有hello world
我要保留第一行的字直接寫From the StreamWriter class放在第二行
我試過再寫第一行時如右 sw.WriteLine(); 結果第一行變成什麼都沒有的空白
我也試過如右 sw.WriteLine("\n"); 結果變成亂碼
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.13.117
→
05/05 08:04, , 1F
05/05 08:04, 1F
推
05/05 09:05, , 2F
05/05 09:05, 2F
→
05/05 09:06, , 3F
05/05 09:06, 3F
推
05/05 09:06, , 4F
05/05 09:06, 4F
→
05/05 09:06, , 5F
05/05 09:06, 5F
※ 編輯: crimesky 來自: 140.112.13.117 (05/05 15:35)
→
05/05 17:58, , 6F
05/05 17:58, 6F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 3 篇):