使用C#替换文本文件中的指定字符


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ReplaceText
{
    class Program
    {
        static void Main(string[] args)
        {
            //防止文本字符中有特殊字符。必须用Encoding.Default
            StreamReader reader = new StreamReader(@"a.hhp", Encoding.Default);

            String a = reader.ReadToEnd();
            //将a.hhp文件中bb替换为cc。
            a = a.Replace("bb", "cc");
           //a.hhp重命名为b.hhp
            //防止文本字符中有特殊字符。必须用Encoding.Default
            StreamWriter readTxt = new StreamWriter(@"b.hhp", false, Encoding.Default);
            readTxt.Write(a);
            readTxt.Flush();
            readTxt.Close();
            reader.Close();
           //b.hhp重命名为a.hhp,并删除b.hhp
            File.Copy(@"b.hhp", @"a.hhp", true);
            File.Delete(@"b.hhp");

        }
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM