2011-10-05 66 views
0

是否有某種方法可以在不創建新對象的情況下重新打開Streamwriter以寫入?因爲此刻,當WriteOdd被調用時,StreamWriter的是覆蓋WriteEven,這之前它被稱爲:/Streamwriter覆蓋txtfile中的文本

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(); 
     } 

感謝

回答

1

你可以通過流對象WriteEven()和WriteOdd()方法。

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