我正在尝试使用 FileStream 写入文件并希望写入第二行,然后写入第一行。我在写完第二行之后使用 Seek() 回到开头,然后写第一行。它取代了第二行(或部分取决于第一行的长度。)我如何不让它取代第二行?
var fs = new FileStream("my.txt", FileMode.Create);
byte[] stringToWrite = Encoding.UTF8.GetBytes("string that should be in the end");
byte[] stringToWrite2 = Encoding.UTF8.GetBytes("first string\n");
fs.Write(stringToWrite, 0, stringToWrite.Length);
fs.Seek(0, SeekOrigin.Begin);
fs.Write(stringToWrite2, 0, stringToWrite2.Length);
以下内容写入文件:
first string
hould be in the end
我希望它是
first string
string that should be in the end
谢谢