• Java遍历目录下全部文件并替换指定字符串


    应用场景:比方有一个深层次的文件目录结构,如:javaAPI

    每一个文件中面都有同样的内容,而我们要统一改动为其它内容。上千个文件假设一个个改动显得太不明智。

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    
    
    public class Test {
        /**
         * @author itmyhome
         */
        public static void main(String[] args) {
            File f = new File("F:/javaAPI/java/lang/reflect");
            print(f, 0);
        }
    
        /**
         * 遍历目录
         *
         * @param f
         * @param len
         */
        public static void print(File f, int len) {
            File[] file = f.listFiles();
    
            for (int i = 0; i < file.length; i++) {
                if (file[i].isDirectory()) { //推断是否目录
                    print(file[i], len + 1);
                }
    
                // 为防止输出文件覆盖源文件,所以更改输出盘路径 也可自行设置其它路径
                File outPath = new File(file[i].getParent().replace("F:", "E:"));
                File readfile = new File(file[i].getAbsolutePath());
    
                if (!readfile.isDirectory()) {
                    String filename = readfile.getName(); // 读到的文件名称
                    String absolutepath = readfile.getAbsolutePath(); // 文件的绝对路径
                    readFile(absolutepath, filename, i, outPath); // 调用 readFile
                }
            }
        }
    
        /**
         * 读取目录下的文件
         *
         * @return
         */
        public static void readFile(String absolutepath, String filename,
            int index, File outPath) {
            try {
                BufferedReader bufReader = new BufferedReader(new InputStreamReader(
                            new FileInputStream(absolutepath), "gb2312")); // 数据流读取文件
    
                StringBuffer strBuffer = new StringBuffer();
                String newStr = "i love you too";
                String oldStr = "i love you";
    
                for (String temp = null; (temp = bufReader.readLine()) != null;
                        temp = null) {
                    if ((temp.indexOf(oldStr) != -1) &&
                            (temp.indexOf(oldStr) != -1)) { // 推断当前行是否存在想要替换掉的字符
                        temp = temp.replace(oldStr, newStr); // 此处进行替换
                    }
    
                    strBuffer.append(temp);
                    strBuffer.append(System.getProperty("line.separator")); // 换行符
                }
    
                bufReader.close();
    
                if (outPath.exists() == false) { // 检查输出目录是否存在,若不存在先创建
                    outPath.mkdirs();
                    System.out.println("已成功创建输出目录:" + outPath);
                }
    
                PrintWriter printWriter = new PrintWriter(outPath + "\" +
                        filename, "gb2312"); // 替换后输出文件路径
                printWriter.write(strBuffer.toString().toCharArray()); //又一次写入
                printWriter.flush();
                printWriter.close();
                System.out.println("第 " + (index + 1) + " 个文件   " + absolutepath +
                    "  已成功输出到    " + outPath + "\" + filename);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    


    作者:itmyhome 


  • 相关阅读:
    HDU
    Groundhog Build Home
    The Moving Points
    Problem I. Count
    Problem E. TeaTree
    树的启发式合并
    Special Segments of Permutation
    网络流24题
    2015-2016 Northwestern European Regional Contest (NWERC 2015)
    The 2018 ACM-ICPC Asia Beijing Regional Contest
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6973552.html
Copyright © 2020-2023  润新知