Sunday, 12 June 2011

Text search and replace in large files using .net framework

When performing search and replace in large text files you should avoid loading the entire file into memory and rather make use of the StreamReader/StreamWriter classes provided by the .Net Framework. I thought I would share this short code snippet below which makes use of these two useful classes.


using System.IO;
namespace LoadXmlTestConsole
{
    public class TextReplaceConsoleApp
    {
        public static void Main()
        {
            // File to perform search and replace on
            string inputFileLocation = @"D:\largeTextFileInput.xml";

            // File to create with replaced text
            string outputFileLocation = @"D:\largeTextFileOutput.xml";

            using (StreamReader reader = new StreamReader(File.Open(inputFileLocation, FileMode.Open)))
            {
                using (StreamWriter writer = new StreamWriter(File.Open(outputFileLocation, FileMode.Create)))
                {
                    ReplaceInStreams(reader, writer);
                }
            }
        }

        private static void ReplaceInStreams(StreamReader reader, StreamWriter writer)
        {
            while (reader.Peek() != -1)
            {
                string line = reader.ReadLine();
                // If search text found
                if (line.IndexOf("Male") > -1)
                {
                    //Perform text replace on line
                    line = line.Replace("Male", "Alien");
                    writer.WriteLine(line);
                }
                else
                {
                    writer.WriteLine(line);
                }
            }
        }
    }
}