zoukankan      html  css  js  c++  java
  • 文件重命名或替换指定内容

    import java.io.BufferedReader;
    import java.io.CharArrayWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class NameUtils {
    
        public static void main(String[] args) throws IOException {
    
            System.out.println("请输入项目名称");
            @SuppressWarnings("resource")
            Scanner sc = new Scanner(System.in);
            String str = sc.nextLine();
            System.out.println("你输入是:" + str);
            String projectName = str;
            String oldName = "city";
            if (str.contains(":")) {
                projectName = str.split(":")[0];
                oldName = str.split(":")[1];
            }
            System.out.println("替换" + oldName + "为" + projectName);
            String projectDir = System.getProperty("user.dir");
            System.out.println("项目路径:" + projectDir);
            File dir = new File(projectDir);
            for (File file : dir.listFiles()) {
                String name = file.getName();
                if (name.contains(oldName)) {
                    System.out.println("开始重命名"+name);
                    name = name.replace(oldName, projectName);
                    String fileName = projectDir+File.separator+ name;
                    boolean flag = file.renameTo(new File(fileName));
                    System.out.println(fileName + "重命名结果"+flag);
                } else if (name.equals("settings.txt")) {
                    System.out.println("开始替换文件内容:"+name);
                    replacTextContent(file.getAbsolutePath(), oldName, projectName);
                    System.out.println("替换文件内容结束:"+name);
                }
            }
            System.out.println("开始重命名文件夹:"+dir.getAbsolutePath());
            String target = dir.getParent() + File.separator + projectName;
            boolean flag = dir.renameTo(new File(target));
            System.out.println(target + "重命名文件夹结束:"+ flag);
        }
    
        /**
         * 替换文本文件中的字符串
         * 
         * @param path
         * @throws IOException
         */
        public static void replacTextContent(String path, String srcStr, String replaceStr) throws IOException {
            //
            File file = new File(path);
            FileReader in = new FileReader(file);
            BufferedReader bufIn = new BufferedReader(in);
            // 内存流, 作为临时流
            CharArrayWriter tempStream = new CharArrayWriter();
            // 替换
            String line = null;
            while ((line = bufIn.readLine()) != null) {
                // 替换每行中, 符合条件的字符串
                line = line.replaceAll(srcStr, replaceStr);
                // 将该行写入内存
                tempStream.write(line);
                // 添加换行符
                tempStream.append(System.getProperty("line.separator"));
            }
            // 关闭 输入流
            bufIn.close();
            // 将内存中的流 写入 文件
            FileWriter out = new FileWriter(file);
            tempStream.writeTo(out);
            out.close();
    
        }
    
    }
  • 相关阅读:
    git 常用操作命令行
    Mysql 命令行...
    bootstrap
    10.11 android输入系统_补充知识_activity_window_decor_view关系
    10.10 android输入系统_APP获得并处理输入事件流程
    10.9 android输入系统_APP跟输入系统建立联系和Dispatcher线程_分发dispatch
    10.8 android输入系统_实战_使用GlobalKey一键启动程序
    10.7 android输入系统_Dispatcher线程情景分析_Reader线程传递事件和dispatch前处理
    10.6 android输入系统_Dispatcher线程_总体框架
    10.5 android输入系统_Reader线程_使用EventHub读取事件和核心类及配置文件_实验_分析
  • 原文地址:https://www.cnblogs.com/liangblog/p/13213584.html
Copyright © 2011-2022 走看看