日期:2014-05-18  浏览次数:21212 次

如何更改一个txt文件内容,让所有字符"aa"前加一个字符"bz"?
例如
我在c:\下有个aaa.txt
里面内容是
fdsfsdfaafff
ccddaa

我想让他运行后变成
fdsfsdfbzaafff
ccddbzaa

如何更改一个txt文件内容,让所有字符 "aa "前加一个字符 "bz "?

------解决方案--------------------
using (StreamReader sr = new StreamReader(@ "c:\test.txt ", System.Text.Encoding.Default))
{
while (sr.Peek() > = 0)
{
string line = sr.ReadLine();
string NewLine=line.replace( "aa ", "bzaa ");
}
}

------解决方案--------------------
string test = "aabbaaddaa ";
string ret = Regex.Replace(test, @ "(aa) ", new MatchEvaluator(mEvaluator));

private string mEvaluator(Match m)
{
return "bz " + m.Groups[1].Value;
}
------解决方案--------------------
System.Text.Encoding.Unicode
------解决方案--------------------
//试试这个
string filePath = Server.MapPath( "TextFile.txt ");  //文件路径
string savedFilePath = Server.MapPath( "Output.txt "); //新文件保存路径

System.IO.StreamReader reader = new System.IO.StreamReader(filePath,System.Text.Encoding.Default);
System.IO.StreamWriter writer = new System.IO.StreamWriter(savedFilePath, false, System.Text.Encoding.Default);

string result = reader.ReadToEnd();
writer.Write(result.Replace( "aa ", "bzaa "));

reader.Close();
writer.Close();
------解决方案--------------------
呵呵,server是asp.net里面的了!
在这里用获取录前目录方法!
------解决方案--------------------
楼上的说的对,
string filePath = //你要更改的那个文件的路径
string savedFilePath = //更改后的文件保存的路径