添加到文本文件而不删除 [英] Adding to text file without deleting
问题描述
我需要将 4 个可变数字添加到一行中,但使用当前代码它只是替换数字,从而使其毫无意义.
I need to add 4 variable numbers into a single line but using the current code it just replaces the numbers thus making it pointless.
using (StreamWriter writer = new StreamWriter("build.txt"))
{
writer.Write(" " + layer + " " + x + " " + y + " " + block);
Console.WriteLine("[" + layer + "," + x + "," + y + "," + block + "]");
}
所以我需要它来写...
so I needed it to write...
l x y b l x y b l x y b l x y b ...
...但它并没有像那样奏效,而且互联网也不是特别有用,希望在这里可以提供帮助.
...but it did not work out like that, and the internet wasn't particularly helpful, hopefully here can help.
推荐答案
我会使用 File.AppendAllText
, 个人... 这样你就可以直接指定你想写的文本,方法会打开文件,追加文本,然后关闭文件.
I would use File.AppendAllText
, personally... That way you can just specify the text you want to write, and the method will open the file, append the text, and then close the file.
如果你真的想使用 writer,你可以使用 File.AppendText
:
If you really want to use the writer though, you could use File.AppendText
:
using (var writer = File.AppendText("build.txt"))
{
writer.Write(...);
}
或者您可以使用 StreamWriter
构造函数和附加
参数.不过,我绝对建议使用 File
中的方法 - 它们更易于阅读.
Or you can use the StreamWriter
constructor with an append
parameter. I'd definitely recommend using the methods in File
though - they're much clearer to read.
这篇关于添加到文本文件而不删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!