2013-03-16 48 views
6

Tôi đang cố gắng tạo một phần mềm đơn giản lưu trữ dữ liệu trong một tệp nhật ký TXT. Đây là mã của tôiFileStream.WriteLine() không ghi vào tập tin

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

Điều tôi đạt được là: tệp được tạo nhưng không có gì được ghi trong đó. Tôi đã kiểm tra rất nhiều bài đăng nhưng tôi không thể tìm thấy bất kỳ trợ giúp cụ thể nào.

+3

Trước hết, try/cuối cùng khối không cần thiết, bạn có thể sử dụng một ' sử dụng 'khối thay thế. – antonijn

+3

bạn có thể cần phải tuôn ra StreamWriter: 'fw.Flush()' –

+0

StreamWriter có một hàm tạo một chuỗi, đường dẫn tới tệp và một bool, được đặt thành true để nối thêm. Nó sẽ giúp bạn rơi vào hố thành công. –

Trả lời

7

Thêm cuộc gọi vào Flush luồng hoạt động. Điều này là do bạn đang gói FileStream. StreamWriter sẽ ghi vào FileStream, nhưng bạn cần phải cho biết thời điểm gửi Stream vào tệp thực tế. Ngoài ra, bạn có thể trao đổi bạn try finally với một using:

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

Kèm theo StreamWriter của bạn trong một khối sử dụng để chắc chắn rằng mọi thứ được đóng một cách chính xác vào cuối việc sử dụng tập tin, tôi cũng không nghĩ rằng bạn cần phải tạo một FileStream để làm việc này.

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

Lưu ý rằng StreamWriter có một constructor mà chấp nhận hai tham số, tên của tập tin để tạo/mở và một lá cờ để chỉ ra rằng các tập tin nên được mở trong chế độ append hoặc ghi đè See StreamWriter docs

1

Thật vậy, Flush() là câu trả lời; tuy nhiên, tôi sẽ sử dụng File.WriteAllLines() để thay thế.

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

Hãy thử sử dụng này - chỉ cần thay thế các mảng:

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

Luôn luôn sử dụng using (như đã đề cập đã được) và bạn sẽ không gặp phải vấn đề (hoặc phải suy nghĩ về nó) ...

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

(cũng bạn có thể đóng cửa nhà văn thay vì FileStream đó nên đã làm việc)

Vấn đề là tôi có thể nói ...

FileStream.Close thực sự là Stream.Close - và gọi số Dispose nhưng không phải là ảo, do đó, một số dọn dẹp chung.

FileStream.Dispose được gọi ngầm khi bạn sử dụng using - thực hiện cụ thể Flush và sau đó Close/Vứt bỏ - do đó, dọn dẹp cụ thể thích hợp.

Bạn có thể tránh được điều đó qua using như thường được khuyến khích mô hình (và thẳng thắn bao giờ đã cho tôi vào bất kỳ trong số này)

Các vấn đề liên quan