有没有办法在没有创建要写入的新对象的情况下重新打开streamwriter?因为此时,当调用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();
}
由于
答案 0 :(得分:4)
使用过载询问您是否要追加
public StreamWriter(
string path,
bool append
)
答案 1 :(得分:1)
您可以将stream对象传递给WriteEven()和WriteOdd()方法。
public void WriteOdd(StreamWiter writer)
{
for (int i = 1; i < array.Length; i += 2)
...
}