FileStreamを使用してファイルに書き込もうとしていますが、2行目を書き込んでから、最初の行を書き込みたいと考えています。2行目を書き込んだ後、Seek()を使用して最初に戻り、最初の行を書き込みます。2行目(または1行目の長さによってはその一部)を置き換えます。2行目を置き換えないようにするにはどうすればよいですか。
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
ありがとう