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

匆匆过客 提交于 2019-12-11 11:22:35

问题


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.


回答1:


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);



回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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);



回答6:


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);


来源:https://stackoverflow.com/questions/5638556/how-can-i-elegantly-implement-multiple-string-replacements-in-the-same-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!