Return StreamReader to Beginning

后端 未结 7 1559
醉梦人生
醉梦人生 2020-11-27 05:18

I\'m reading a file in line-by-line and I want to be able to restart the read by calling a method Rewind().

How can I manipulate my System.IO.Stre

7条回答
  •  半阙折子戏
    2020-11-27 05:46

    public static void removeDuplicatedLinesBigFile2(string inFile, string outFile)
    {
        int counter1 = 0, counter2 = 0;
        string line1, line2;
        bool band = false;
    
        // Read the file and display it line by line.  
        System.IO.StreamReader fileIN1 = new System.IO.StreamReader(inFile);
        System.IO.StreamReader fileIN2 = new System.IO.StreamReader(inFile);
        System.IO.StreamWriter fileOut = new System.IO.StreamWriter(outFile);
    
        while ((line1 = fileIN1.ReadLine()) != null)
        {
            //band = false;
            int counter = 0;
            fileIN2.BaseStream.Position = 0;
            fileIN2.DiscardBufferedData();
    
            while ((line2 = fileIN2.ReadLine()) != null)
            {                   
                if (line1.Equals(line2))
                    counter++;
    
                if (counter > 1)
                    break;
            }
    
            fileOut.WriteLine(line1);
            counter1++;
        }
    
        fileIN1.Close();
        fileIN2.Close();
        Console.WriteLine("Total Text Rows Copied: {0}", counter1);
    }
    

提交回复
热议问题