当前位置:编程学习 > C#/ASP.NET >>

关于文本文件操作的问题

请问谁知道一个写好的文本文件,怎么对其中指定位置开始,固定长度的一段内容进行修改? --------------------编程问答-------------------- string text = File.ReadAllText("test.txt", Encoding.GetEncoding("gbk"));
获取到字符串后自己就可以任意处理了。 --------------------编程问答-------------------- FileStream.Seek 定位到指定位置,然后直接写
当然了你说的固定长度必须是按照字节算的,按照字符算的不行,只能一次性读取、替换,再写回,不能直接修改。 --------------------编程问答-------------------- 查找并替换,再保存
 string path = @"test.txt";
      string con = "";
      FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
      StreamReader sr = new StreamReader(fs);
      con = sr.ReadToEnd();
      con = con.Replace("1", "10");//找到字符“1”替换为“10”
      sr.Close();
      fs.Close();
      FileStream fs2 = new FileStream(path, FileMode.Open, FileAccess.Write);
      StreamWriter sw = new StreamWriter(fs2);
      sw.WriteLine(con);
      sw.Close();
      fs2.Close();
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,