开发者

Streamwriter overwriting the text in txtfile

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


Use the overload that asks you if you want to append

public StreamWriter(
    string path,
    bool append
)

http://msdn.microsoft.com/en-us/library/36b035cb.aspx


You can pass stream object to WriteEven() and WriteOdd() methods.

public void WriteOdd(StreamWiter writer)
{
  for (int i = 1; i < array.Length; i += 2)
  ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜