2013-03-16 81 views
6

我想製作一個簡單的軟件,它將數據存儲在TXT日誌文件中。 這是我的代碼FileStream.WriteLine()不寫入文件

FileStream fs = null; 
StreamWriter fw = null; 
try 
{ 
    fs= new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/textme.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); 
    fw = new StreamWriter(fs); 
    fw.Write("sadadasdsadsadsadas"); 

    for (int i = 0; i < AnimalShelter.AnimalList.Count; i++) 
    { 
     fw.WriteLine("<chipNr>" + AnimalShelter.AnimalList[i].ChipRegistrationNumber + "<chipNr>"); 
     Console.WriteLine("<chipNr>" + AnimalShelter.AnimalList[i].ChipRegistrationNumber + "<chipNr>"); 
    } 
} 
catch(IOException) 
{ 
    MessageBox.Show("ERROR THROWN"); 
} 
finally 
{ 
    if (fs!= null) fs.Close(); 
    // if (fw != null) fw.Close(); 
} 

我取得了什麼是:文件被創建,但沒有被寫在上面。 我查了很多帖子,但找不到任何特別的幫助。

+3

首先,不需要在try/finally塊,你可以使用'使用'代替。 – antonijn 2013-03-16 18:18:10

+3

您可能需要刷新StreamWriter:'fw.Flush()' – 2013-03-16 18:18:12

+0

StreamWriter具有一個構造函數,它接受一個字符串,該文件的路徑和一個布爾值,設置爲true以追加。它會幫助你陷入成功的陷阱。 – 2013-03-16 18:33:56

回答

7

添加對Flush流的調用。這是因爲你正在包裝FileStreamStreamWriter將寫入FileStream,但您需要指出何時將Stream發送到實際文件。此外,您還可以交換你的try finallyusing

try 
{ 
    using (var fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/textme.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)) 
    { 
     using (var fw = new StreamWriter(fs)) 
     { 
      fw.Write("sadadasdsadsadsadas"); 

      for (int i = 0; i < AnimalShelter.AnimalList.Count; i++) 
      { 
       fw.WriteLine("<chipNr>" + AnimalShelter.AnimalList[i].ChipRegistrationNumber + "<chipNr>"); 
       Console.WriteLine("<chipNr>" + AnimalShelter.AnimalList[i].ChipRegistrationNumber + "<chipNr>"); 

      } 
      fw.Flush(); // Added 
     } 
    } 
} 
catch(IOException) 
{ 
    MessageBox.Show("ERROR THROWN"); 
} 
4

附上您的StreamWriter在使用塊,以確保一切正常收於文件使用的結束,也是我不認爲你需要創建一個FileStream這個工作。

try 
{ 
    string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "textme.txt") 
    using(fw = new StreamWriter(fileName, true)) 
    { 
     ...... 
    } 
} 
catch(IOException) 
{ 
    MessageBox.Show("ERROR THROWN"); 
} 

注意,StreamWriter有兩個參數,文件的名稱,以創建/打開一個標誌,用來表明該文件應以追加方式打開或覆蓋構造 See StreamWriter docs

1

事實上,Flush()就是答案;不過,我會用File.WriteAllLines()代替。

try 
{ 
    var fileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/textme.txt"; 
    var lines = AnimalShelter.AnimalList.Select(o=> "<chipNr>" + o.ChipRegistrationNumber + "</chipNr>"); 
    File.WriteAllLines(fileName, lines); 
    foreach(var line in lines) 
     Console.WriteLine(line); 
} 
catch(IOException) 
{ 
    MessageBox.Show("ERROR THROWN"); 
} 
0

嘗試使用此 - 只需更換陣列:

try 
{ 
    using (Stream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/textme.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)) 
    { 
     using (StreamWriter sw = new StreamWriter(fs)) 
     { 
      int[] test = new int[] { 0, 12, 23, 46 }; 
      sw.Write("sadadasdsadsadsadas"); 
      for (int i = 0; i < test.Length; i++) 
      { 
       sw.WriteLine("<chipNr>" + test[i] + "<chipNr>"); 
       Console.WriteLine("<chipNr>" + test[i] + "<chipNr>"); 
      } 
      sw.Close();      
     } 
     fs.Close(); 
    } 

} 
catch (IOException) 
{ 
    MessageBox.Show("ERROR THROWN"); 
} 
2

始終使用using(前面已經提到),你會不會有問題(或不得不考慮一下)......

using (FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/textme.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)) 
using (StreamWriter fw = new StreamWriter(fs)) 
{ 
    fw2.Write("sadadasdsadsadsadas"); 
} 

(也可能你已經關閉,而不是FILESTREAM應該已經工作的作家)

問題是,我只要我可以告訴...

FileStream.Close實際上是Stream.Close - 和調用Dispose但它不是虛擬的,所以做了一些常規清理。

FileStream.Dispose當您使用using這是隱式調用 - 不特定Flush然後Close /處置 - 也是如此恰當的具體清理。

您可以通過using爲避免任何的是,一般建議模式(坦白地說從來沒有讓我到任何這些)