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"); } }
我是楼主 我也能得到字符 但是怎么样把改的内容保存到txt里啊
知道了 就是 但是汉字是?? 该怎么解决啊? 我记得有个是什么encoding等 string s = null; using (StreamReader sr = new System.IO.StreamReader(@"c:\index.txt")) { s = sr.ReadToEnd(); s = s.Replace("</html>", "ff</html>"); } using (StreamWriter sw = new StreamWriter(@"c:\index.txt", false, Encoding.Default)) { sw.WriteLine(s); }
string test = "aabbaaddaa"; string ret = Regex.Replace(test, @"(aa)", new MatchEvaluator(mEvaluator)); private string mEvaluator(Match m) { return "bz" + m.Groups[1].Value; }
//试试这个 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();
不好意思 楼上的 using 什么啊 找不到server~~~
不好意思 楼上的 using 什么啊 找不到server~~~ ===============> 说清楚一点
{
while (sr.Peek() >= 0)
{
string line = sr.ReadLine();
string NewLine=line.replace("aa","bzaa");
}
}
我也能得到字符
但是怎么样把改的内容保存到txt里啊
但是汉字是?? 该怎么解决啊?
我记得有个是什么encoding等
string s = null;
using (StreamReader sr = new System.IO.StreamReader(@"c:\index.txt"))
{
s = sr.ReadToEnd();
s = s.Replace("</html>", "ff</html>");
}
using (StreamWriter sw = new StreamWriter(@"c:\index.txt", false, Encoding.Default))
{
sw.WriteLine(s);
}
string ret = Regex.Replace(test, @"(aa)", new MatchEvaluator(mEvaluator)); private string mEvaluator(Match m)
{
return "bz" + m.Groups[1].Value;
}
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~~~
找不到server~~~
===============>
说清楚一点
string savedFilePath = Server.MapPath("Output.txt"); //新文件保存路径报错 说 找不到 server
在这里用获取录前目录方法!
string filePath = //你要更改的那个文件的路径
string savedFilePath = //更改后的文件保存的路径