Matthew Steven Monkan
Matthew Steven Monkan

Reputation: 9130

How can I elegantly implement multiple string replacements in the same file?

Currently I have some code to replace strings in a file that looks like this:

File.WriteAllText(filePath, Regex.Replace(File.ReadAllText(filePath),
    "( " + column.Key + " )",
    " " + column.Value + " "
));
File.WriteAllText(filePath, Regex.Replace(File.ReadAllText(filePath),
    "(\\[\"" + column.Key + "\"\\])",
    "[\"" + column.Value + "\"]"
));

However, each replacement opens and closes the file, and it seems that occasionally they run "too fast" and one replacement will not work because the file did not close yet in a previous string replacement. Is there any code I can reuse that solves this issue, perhaps using the FileStream class (so I can open and close once)? Or suggestions on a better way to do this? Just wondering if there is something simpler I can do than having to create byte arrays of the strings I want to replace and writing code to read, write, and seek through bytes manually. Thanks.

Upvotes: 1

Views: 1294

Answers (6)

Jamiec
Jamiec

Reputation: 136114

Well, the simlest method would be to ReadAllText, do your replacements and then WriteAllText.

var text = File.ReadAllText(filePath);
text = Regex.Replace(text,"( " + column.Key + " )"," " + column.Value + " ");
text = Regex.Replace(text,"(\\[\"" + column.Key + "\"\\])","[\"" + column.Value + "\"]");
File.WriteAllText(text,filePath);

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190941

var fileContents = File.ReadAllText(filePath);
fileContents = Regex.Replace(fileContents,
    "( " + column.Key + " )",
    " " + column.Value + " "
);
fileContents = Regex.Replace(fileContents ,
    "(\\[\"" + column.Key + "\"\\])",
    "[\"" + column.Value + "\"]"
);
File.WriteAllText(filePath, fileContents);

Upvotes: 1

Shaun Bowe
Shaun Bowe

Reputation: 10008

string contents = File.ReadAllText(filePath);
contents = Regex.Replace(contents,
    "( " + column.Key + " )",
    " " + column.Value + " ");
contents = Regex.Replace(contents,
    "(\\[\"" + column.Key + "\"\\])",
    "[\"" + column.Value + "\"]");
File.WriteAllText(filePath, contents);

Upvotes: 2

iZ.
iZ.

Reputation: 5041

Sounds like you should be doing your all your string replacements on a string residing in memory, then write your final resulting string to disk.

Upvotes: 1

Thebigcheeze
Thebigcheeze

Reputation: 3488

A better practice would be to read the contents of the file once, storing it into a local variable. Then performing any changes you need (in your case, two regular expressions), and then writing that output to the file. File IO is one of the most expensive operations a computer can perform, and in-memory computation is much cheaper. Hit the disk as little as possible, as long as you can avoid it.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1500805

Well, I'd use:

 string text = File.ReadAllText(filePath);

 text = Regex.Replace(...);
 text = Regex.Replace(...);
 ...
 File.WriteAllText(filePath, text);

I'm still surprised to hear that the original code didn't work though. It wasn't pleasant in terms of multiple writes and reads, but I'd have expected it to work.

Upvotes: 3

Related Questions