Streamwriter 覆盖 txt 文件中的文本
有没有什么方法可以重新打开流写入器而不创建新的写入对象?因为此时,当调用 WriteOdd 时,streamwriter 正在覆盖在它之前调用的 WriteEven :/
public void WriteEven()
{
StreamWriter writer = new StreamWriter(FILENAME);
for (int i = 0; i < array.Length; i+= 2)
{
Console.WriteLine(array[i]);
writer.WriteLine("EvenNumbers: " + array[i]);
}
writer.Close();
}
public void WriteOdd()
{
StreamWriter writer = new StreamWriter(FILENAME);
for (int i = 1; i < array.Length; i += 2)
{
Console.WriteLine(array[i]);
writer.WriteLine("OddNumbers: " + array[i]);
}
writer.Close();
}
谢谢
Is there some way to reopen streamwriter without it creating a new object to write to? Because at the moment, when WriteOdd is called, streamwriter is overwriting WriteEven, which is called before it :/
public void WriteEven()
{
StreamWriter writer = new StreamWriter(FILENAME);
for (int i = 0; i < array.Length; i+= 2)
{
Console.WriteLine(array[i]);
writer.WriteLine("EvenNumbers: " + array[i]);
}
writer.Close();
}
public void WriteOdd()
{
StreamWriter writer = new StreamWriter(FILENAME);
for (int i = 1; i < array.Length; i += 2)
{
Console.WriteLine(array[i]);
writer.WriteLine("OddNumbers: " + array[i]);
}
writer.Close();
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用重载,询问您是否要附加
http://msdn.microsoft。 com/en-us/library/36b035cb.aspx
Use the overload that asks you if you want to append
http://msdn.microsoft.com/en-us/library/36b035cb.aspx
您可以将流对象传递给 WriteEven() 和 WriteOdd() 方法。
You can pass stream object to WriteEven() and WriteOdd() methods.