个人写的文本替换小程序

import java.io.*;import java.util.HashMap;import java.util.Map;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Database2DatabaseUtil {    //输出计数,方便观察    private static int count = 0;    public static void main(String[] args) throws IOException {        //只需替换Map即可        replaceAllFile("D:\\ningxinjie\\code\\b2bcode\\util\\file",                "D:\\ningxinjie\\code\\b2bcode\\util\\new-File", design2OnlineMap);    }    //design切online    static Map<String, String> design2OnlineMap = new HashMap<>();    //使用示例:design2OnlineMapPattern.put("cms01","cms01(!A)")    static Map<String, String> design2OnlineMapPattern = new HashMap<>();    //online切ck    static Map<String, String> doris2CkMap = new HashMap<>();    static Map<String, String> doris2CkMapPattern = new HashMap<>();    //design切ck    static Map<String, String> design2DorisMap = new HashMap<>();    static Map<String, String> design2DorisMapPattern = new HashMap<>();    /**     * 替换所有新文件     *     * @param rootPath     * @param targetPath     * @param map     * @return     */    public static boolean replaceAllFile(String rootPath, String targetPath, Map<String, String> map) {        return replaceAllFile(rootPath, targetPath, map, null);    }    /**     * 替换所有新文件(包含正则)     *     * @param rootPath   原位置     * @param targetPath 目标位置     * @param map        替换选项,【如果为null则复制】     * @param mapPattern 正则选项,【如果为null则不使用正则】     *                   内部调用 replaceTxt     * @return     */    public static boolean replaceAllFile(String rootPath, String targetPath, Map<String, String> map, Map<String, String> mapPattern) {        File file = new File(rootPath);        if (file.isFile()) {            replaceTxt(rootPath, targetPath, true, map, mapPattern);        } else if (file.isDirectory()) {            //确保目录存在            File targetFile = new File(targetPath);            if (!targetFile.exists() && !targetFile.isDirectory()) targetFile.mkdir();            String[] list = file.list();            for (String s : list) {                replaceAllFile(rootPath   "\\"   s, targetPath   "\\"   s, map, mapPattern);            }        } else {            System.out.println("输入异常..."   rootPath);            return false;        }        return true;    }    /**     * @param oldfilePath 源文本地址     * @param newfilePath 新文本地址     * @param isOverride  新文本如果存在是否覆盖     * @param map         要替换的map     * @param mapPattern  正则替换(为了处理一些假如cms03 与 cms03A 我们想替换不影响,这时候就需要如cms03(?!A)这样的正则表达式来规定,如果不需要传null即可)     * @return     */    public static boolean replaceTxt(String oldfilePath, String newfilePath, boolean isOverride, Map<String, String> map, Map<String, String> mapPattern) {        if (oldfilePath.equals("D:\\ningxinjie\\code\\b2bcode\\util\\abc\\dimension\\FindDimension.java")) {            System.out.println(1);        }        File file = new File(oldfilePath);        File newfile = new File(newfilePath);        if (!isOverride && newfile.exists()) {            System.out.println("源文件存在");            return false;        }        //判断文件存在并且是文件        Boolean isFile = file.exists() && file.isFile();        if (isFile) {            BufferedReader bufferedReader = null;            BufferedWriter bufferedWriter = null;            try {                //构造一个BufferedReader类来读取文件                bufferedReader = new BufferedReader(new FileReader(file));                //构建一个BufferedWriter类来写文件                bufferedWriter = new BufferedWriter(new FileWriter(newfilePath));                String linetxt = null;                //result用来存储文件内容                StringBuilder result = new StringBuilder();                //按使用readLine方法,一次读一行                while ((linetxt = bufferedReader.readLine()) != null) {                    String newcontent = linetxt;                    if (map != null) {                        Set<Map.Entry<String, String>> entries = map.entrySet();                        for (Map.Entry<String, String> entry : entries) {                            //判断是否需要使用正则                            if (mapPattern != null && mapPattern.containsKey(entry.getKey())) {                                newcontent = regReplace(newcontent, mapPattern.get(entry.getKey()), entry.getValue());                            } else {                                newcontent = newcontent.replace(entry.getKey(), entry.getValue());//替换                            }                        }                    }                    bufferedWriter.write(newcontent   "\n");//\r\n                    //bufferedWriter.newLine();                }            } catch (Exception e) {                System.out.println("读取文件内容出错");                e.printStackTrace();            } finally {                try {                    bufferedWriter.close();                    bufferedReader.close();                } catch (IOException e1) {                    e1.printStackTrace();                }            }        } else {            System.out.println("输入异常!源目录不是一个文件!");            return false;        }        System.out.println("数据输出完成"   (  count));        return true;    }    /**     * 正则使用     *     * @param content   原文本     * @param pattern   正则表达式     * @param newString 正则匹配项替换文本     * @return     */    public static String regReplace(String content, String pattern, String newString) {        Pattern p = Pattern.compile(pattern);        Matcher m = p.matcher(content);        String result = m.replaceAll(newString);        return result;    }    /**     * 创建空文件(临时创建文件,接收文本使用)     *     * @param path      路径     * @param fileNames 名字集合     * @throws IOException     */    public static void createNewFile(String path, String[] fileNames) throws IOException {        for (String fileName : fileNames) {            File file = new File(path   "\\f-"   fileName);            file.createNewFile();        }    }}

View Code

import java.io.*;import java.util.HashMap;import java.util.Map;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Database2DatabaseUtil {    //输出计数,方便观察    private static int count = 0;    public static void main(String[] args) throws IOException {        //只需替换Map即可        replaceAllFile("D:\\ningxinjie\\code\\b2bcode\\util\\file",                "D:\\ningxinjie\\code\\b2bcode\\util\\new-File", design2OnlineMap);    }    //design切online    static Map<String, String> design2OnlineMap = new HashMap<>();    //使用示例:design2OnlineMapPattern.put("cms01","cms01(!A)")    static Map<String, String> design2OnlineMapPattern = new HashMap<>();    //online切ck    static Map<String, String> doris2CkMap = new HashMap<>();    static Map<String, String> doris2CkMapPattern = new HashMap<>();    //design切ck    static Map<String, String> design2DorisMap = new HashMap<>();    static Map<String, String> design2DorisMapPattern = new HashMap<>();    /**     * 替换所有新文件     *     * @param rootPath     * @param targetPath     * @param map     * @return     */    public static boolean replaceAllFile(String rootPath, String targetPath, Map<String, String> map) {        return replaceAllFile(rootPath, targetPath, map, null);    }    /**     * 替换所有新文件(包含正则)     *     * @param rootPath   原位置     * @param targetPath 目标位置     * @param map        替换选项,【如果为null则复制】     * @param mapPattern 正则选项,【如果为null则不使用正则】     *                   内部调用 replaceTxt     * @return     */    public static boolean replaceAllFile(String rootPath, String targetPath, Map<String, String> map, Map<String, String> mapPattern) {        File file = new File(rootPath);        if (file.isFile()) {            replaceTxt(rootPath, targetPath, true, map, mapPattern);        } else if (file.isDirectory()) {            //确保目录存在            File targetFile = new File(targetPath);            if (!targetFile.exists() && !targetFile.isDirectory()) targetFile.mkdir();            String[] list = file.list();            for (String s : list) {                replaceAllFile(rootPath   "\\"   s, targetPath   "\\"   s, map, mapPattern);            }        } else {            System.out.println("输入异常..."   rootPath);            return false;        }        return true;    }    /**     * @param oldfilePath 源文本地址     * @param newfilePath 新文本地址     * @param isOverride  新文本如果存在是否覆盖     * @param map         要替换的map     * @param mapPattern  正则替换(为了处理一些假如cms03 与 cms03A 我们想替换不影响,这时候就需要如cms03(?!A)这样的正则表达式来规定,如果不需要传null即可)     * @return     */    public static boolean replaceTxt(String oldfilePath, String newfilePath, boolean isOverride, Map<String, String> map, Map<String, String> mapPattern) {        if (oldfilePath.equals("D:\\ningxinjie\\code\\b2bcode\\util\\abc\\dimension\\FindDimension.java")) {            System.out.println(1);        }        File file = new File(oldfilePath);        File newfile = new File(newfilePath);        if (!isOverride && newfile.exists()) {            System.out.println("源文件存在");            return false;        }        //判断文件存在并且是文件        Boolean isFile = file.exists() && file.isFile();        if (isFile) {            BufferedReader bufferedReader = null;            BufferedWriter bufferedWriter = null;            try {                //构造一个BufferedReader类来读取文件                bufferedReader = new BufferedReader(new FileReader(file));                //构建一个BufferedWriter类来写文件                bufferedWriter = new BufferedWriter(new FileWriter(newfilePath));                String linetxt = null;                //result用来存储文件内容                StringBuilder result = new StringBuilder();                //按使用readLine方法,一次读一行                while ((linetxt = bufferedReader.readLine()) != null) {                    String newcontent = linetxt;                    if (map != null) {                        Set<Map.Entry<String, String>> entries = map.entrySet();                        for (Map.Entry<String, String> entry : entries) {                            //判断是否需要使用正则                            if (mapPattern != null && mapPattern.containsKey(entry.getKey())) {                                newcontent = regReplace(newcontent, mapPattern.get(entry.getKey()), entry.getValue());                            } else {                                newcontent = newcontent.replace(entry.getKey(), entry.getValue());//替换                            }                        }                    }                    bufferedWriter.write(newcontent   "\n");//\r\n                    //bufferedWriter.newLine();                }            } catch (Exception e) {                System.out.println("读取文件内容出错");                e.printStackTrace();            } finally {                try {                    bufferedWriter.close();                    bufferedReader.close();                } catch (IOException e1) {                    e1.printStackTrace();                }            }        } else {            System.out.println("输入异常!源目录不是一个文件!");            return false;        }        System.out.println("数据输出完成"   (  count));        return true;    }    /**     * 正则使用     *     * @param content   原文本     * @param pattern   正则表达式     * @param newString 正则匹配项替换文本     * @return     */    public static String regReplace(String content, String pattern, String newString) {        Pattern p = Pattern.compile(pattern);        Matcher m = p.matcher(content);        String result = m.replaceAll(newString);        return result;    }    /**     * 创建空文件(临时创建文件,接收文本使用)     *     * @param path      路径     * @param fileNames 名字集合     * @throws IOException     */    public static void createNewFile(String path, String[] fileNames) throws IOException {        for (String fileName : fileNames) {            File file = new File(path   "\\f-"   fileName);            file.createNewFile();        }    }}

来源:https://www.icode9.com/content-1-781451.html

(0)

相关推荐