zoukankan      html  css  js  c++  java
  • 批量替换文件夹里面的文本文件的指定字符

    View Code
     1         ///<summary>
    2 /// 初始化加载
    3 ///</summary>
    4 public static void Update()
    5 {
    6 //文件夹路径
    7 string path = @"D:\408";
    8 //获取文件夹
    9 DirectoryInfo dir = new DirectoryInfo(path);
    10 GetFileInfo(path, dir);
    11
    12 }
    13 ///<summary>
    14 /// 遍历文件夹下面的子文件
    15 ///</summary>
    16 ///<param name="pathFile">文件夹路径</param>
    17 ///<param name="dir">文件夹名</param>
    18 private static void GetFileInfo(string pathFile, DirectoryInfo dir)
    19 {
    20
    21 //获取文件夹下面的子文件
    22 FileInfo[] dirInfo = dir.GetFiles();
    23 DataTable dt = new DataTable();
    24 //遍历子文件
    25 foreach (FileInfo info in dirInfo)
    26 {
    27 //子文件名称
    28 string filename = info.Name.ToString();
    29 Console.WriteLine(filename);
    30 htm(@"D:\408\" + filename);
    31
    32 }
    33
    34
    35 }
    36 ///<summary>
    37 /// 替换文件
    38 ///</summary>
    39 ///<param name="path">文本文件路径</param>
    40 public static void htm(string path)
    41 {
    42 string html = "";
    43 using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
    44 {
    45 using (StreamReader reader = new StreamReader(stream))
    46 {
    47 html = reader.ReadToEnd().Trim();
    48 html = html.Replace("", "D").Replace("", "B").Replace("", "A").Replace("", "C");
    49
    50
    51 }
    52 }
    53 DataInit(html, path);
    54 }
    55 ///<summary>
    56 /// 保存问题换过的文本文件
    57 ///</summary>
    58 ///<param name="html">替换过的文件内容</param>
    59 ///<param name="path">保存路径</param>
    60 public static void DataInit(string html, string path)
    61 {
    62
    63 using (Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write))
    64 {
    65 using (StreamWriter writer = new StreamWriter(stream))
    66 {
    67 writer.Write(html);
    68 }
    69 }
    70 }
  • 相关阅读:
    stylelint 安装配置
    使用 jest 测试 react component 的配置,踩坑。
    互联网媒体类型 MIME Type
    react-router 父子路由同时要接收 params 的写法
    fixed 相对于父容器定位
    react 点击空白处隐藏弹出层
    canvas 使用 isPointInPath() 判断鼠标位置是否在绘制的元素上
    JavaScript 缓存基本原理
    简单说明 Virtual DOM 为啥快
    通过阻止 touchstart 事件,解决移动端的 BUG
  • 原文地址:https://www.cnblogs.com/happygx/p/2244869.html
Copyright © 2011-2022 走看看