Reputation: 397
As title, I'm trying to replace 2 or more space between characters into a single space. The following code works, however, not for very large input file. How can I make it works for huge input file as well?
static void Main(string[] args)
{
Regex pattern = new Regex(@"[ ]{2,}"); //Pattern = 2 or more space in a string.
StreamReader reader = new StreamReader(@"C:\CSharpProject\in\abc.txt");
string content = reader.ReadToEnd();
reader.Close();
content = pattern.Replace(content, @" "); //Replace 2 or more space into a single space.
StreamWriter writer = new StreamWriter(@"C:\CSharpProject\out\abc.txt");
writer.Write(content);
writer.Close();
}
Upvotes: 1
Views: 171
Reputation: 27923
Line-by-line, like this:
static void Main(string[] args)
{
Regex pattern = new Regex(@"[ ]{2,}"); //Pattern = 2 or more space in a string.
using (StreamReader reader = new StreamReader(@"C:\CSharpProject\in\abc.txt"))
using (StreamWriter writer = new StreamWriter(@"C:\CSharpProject\out\abc.txt"))
{
string content;
while (null != (content = reader.ReadLine()));
writer.WriteLine (pattern.Replace (content, " "));
writer.Close();
reader.Close();
}
}
Upvotes: 2
Reputation: 3892
The file is being read all at once. There are limits. Instead of reader.ReaderToEnd(), use reader.ReadLine() and read and process the file one line at a time. Or if the file doesn't have "lines" read the input file in chunks and save the output chunks as it is being processed.
Upvotes: 0