我正在將輸出寫入此文件,但它在Organized.txt中始終顯示爲空。如果我從sr2.WriteLine更改最後一個foreach循環,並且只使用WriteLine寫入控制檯,那麼輸出在控制檯上正確顯示,那麼爲什麼它不能正確顯示在文本文件中?寫入文件但文件爲空
class Program
{
public static void Main()
{
string[] arr1 = new string[200];
System.IO.StreamWriter sr2 = new System.IO.StreamWriter("OrganizedVersion.txt");
// Dictionary, key is number from the list and the associated value is the number of times the key is found
Dictionary<string, int> occurrences = new Dictionary<string, int>();
// Loop test data
foreach (string value in File.ReadLines("newWorkSheet.txt"))
{
if (occurrences.ContainsKey(value)) // Check if we have found this key before
{
// Key exists. Add number of occurrences for this key by one
occurrences[value]++;
}
else
{
// This is a new key so add it. Number 1 indicates that this key has been found one time
occurrences.Add(value, 1);
}
}
// Dump result
foreach (string key in occurrences.Keys)
{
sr2.WriteLine(key.ToString() + occurrences[key].ToString());
}
Console.ReadLine();
}
}
我在代碼中找不到任何'Organized.txt' – 2013-05-13 18:37:33
'flush'輸出緩衝區或'關閉'流。 – cgTag 2013-05-13 18:37:53