我正在尝试读取一个大文本文件(14MB),将每一行放入一个字符串列表,然后从中取出不同的字符串,然后将其写回另一个文本文件,我使用以下代码:
    static void removeDuplicates(string filename)
    {
        //Reading from the file
        Console.WriteLine("Reading from the file....");
        StreamReader sr = new StreamReader(filename);
        List<string> namesList = new List<string>();
        while (!sr.EndOfStream)
        {
            namesList.Add(sr.ReadLine());
        }
        //Getting the distinct list
        namesList=namesList.Distinct().ToList<string>();           
        Console.WriteLine("Writing to the new file");
        //writing back to the file
        StreamWriter sw = new StreamWriter(filename + "_NoDuplicates",false);
        for (int i = 0; i < namesList.Count; i++)
        {               
            sw.Write(namesList[i] + "\r\n");
        }
    }
问题是streamWriter总是在一定数量的行之后停止写入,总是在同一个地方停止写入
我确保列表内容正确,并且循环遍历列表中的所有项目,这只是 streamWriter 问题。
该列表在 Distinct() 之前包含 1048577 个项目,在 Distinct() 之后包含 880829 个项目;
streamWriter 在字符串编号 880805 的中间停止写入,之后不再写入任何内容,它甚至在一个单词的中间停止!
为什么会这样,我做错了什么?