小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

個(gè)人寫的文本替換小程序

 印度阿三17 2020-12-10
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 {

    //輸出計(jì)數(shù),方便觀察
    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 目標(biāo)位置
     * @param map        替換選項(xiàng),【如果為null則復(fù)制】
     * @param mapPattern 正則選項(xiàng),【如果為null則不使用正則】
     *                   內(nèi)部調(diào)用 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 我們想替換不影響,這時(shí)候就需要如cms03(?!A)這樣的正則表達(dá)式來規(guī)定,如果不需要傳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 {
                //構(gòu)造一個(gè)BufferedReader類來讀取文件
                bufferedReader = new BufferedReader(new FileReader(file));
                //構(gòu)建一個(gè)BufferedWriter類來寫文件
                bufferedWriter = new BufferedWriter(new FileWriter(newfilePath));

                String linetxt = null;
                //result用來存儲文件內(nèi)容
                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("讀取文件內(nèi)容出錯(cuò)");
                e.printStackTrace();
            } finally {
                try {
                    bufferedWriter.close();
                    bufferedReader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        } else {
            System.out.println("輸入異常!源目錄不是一個(gè)文件!");
            return false;
        }
        System.out.println("數(shù)據(jù)輸出完成"   (  count));
        return true;
    }

    /**
     * 正則使用
     *
     * @param content   原文本
     * @param pattern   正則表達(dá)式
     * @param newString 正則匹配項(xiàng)替換文本
     * @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;
    }

    /**
     * 創(chuàng)建空文件(臨時(shí)創(chuàng)建文件,接收文本使用)
     *
     * @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 {

    //輸出計(jì)數(shù),方便觀察
    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 目標(biāo)位置
     * @param map        替換選項(xiàng),【如果為null則復(fù)制】
     * @param mapPattern 正則選項(xiàng),【如果為null則不使用正則】
     *                   內(nèi)部調(diào)用 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 我們想替換不影響,這時(shí)候就需要如cms03(?!A)這樣的正則表達(dá)式來規(guī)定,如果不需要傳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 {
                //構(gòu)造一個(gè)BufferedReader類來讀取文件
                bufferedReader = new BufferedReader(new FileReader(file));
                //構(gòu)建一個(gè)BufferedWriter類來寫文件
                bufferedWriter = new BufferedWriter(new FileWriter(newfilePath));

                String linetxt = null;
                //result用來存儲文件內(nèi)容
                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("讀取文件內(nèi)容出錯(cuò)");
                e.printStackTrace();
            } finally {
                try {
                    bufferedWriter.close();
                    bufferedReader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        } else {
            System.out.println("輸入異常!源目錄不是一個(gè)文件!");
            return false;
        }
        System.out.println("數(shù)據(jù)輸出完成"   (  count));
        return true;
    }

    /**
     * 正則使用
     *
     * @param content   原文本
     * @param pattern   正則表達(dá)式
     * @param newString 正則匹配項(xiàng)替換文本
     * @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;
    }

    /**
     * 創(chuàng)建空文件(臨時(shí)創(chuàng)建文件,接收文本使用)
     *
     * @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./content-1-781451.html

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多