需求:查找后台代码中写的SQL是否包含拆分表,如果存在,则返回那些表名

 

Context.txt 粘贴我们找到的DAO层代码,因为所有方法封装的SQL都在DAO层里【就理解为筛查的字符串】

Dictionary.txt 存放了拆分表的名字,也就是字典

Output.log 输出日志

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import java.time.format.FormatStyle;import java.util.*;import java.util.logging.Logger;public class Main {    private static Logger logger = Logger.getLogger(Main.class.getName());    // 当前程序所在路径
    private static final String LOCAL_PATH = System.getProperty("user.dir");    private static final String CONTEXT_FILE_PATH = LOCAL_PATH + "\\Context.txt";    private static final String DICTIONARY_FILE_PATH = LOCAL_PATH + "\\Dictionary.txt";    private static final String OUTPUT_FILE_PATH = LOCAL_PATH + "\\Output.log";    public static void main(String[] args) throws Exception {


        FileReader contextFileReader = new FileReader(CONTEXT_FILE_PATH);
        BufferedReader contextBufferedReader = new BufferedReader(contextFileReader);

        FileReader dictionaryFileReader = new FileReader(DICTIONARY_FILE_PATH);
        BufferedReader dictionaryBufferedReader = new BufferedReader(dictionaryFileReader);

        FileWriter outputFileWriter = new FileWriter(OUTPUT_FILE_PATH);
        BufferedWriter outputBufferedWriter = new BufferedWriter(outputFileWriter);        // 加载字典
        String dictString = dictionaryBufferedReader.readLine();        // 封装结构
        ListdictList = Arrays.asList(dictString.split(","));        // 扫描的context行
        String tempRow = "";

        LinkedHashMaplinkedHashMap = new LinkedHashMap();
        linkedHashMap.put("flag", false);

        LinkedList linkedList = new LinkedList();        // 对context进行扫描
        while (null != (tempRow = contextBufferedReader.readLine())) {            // 创建字典的迭代器
            Iteratoriterator = dictList.iterator();            // 迭代扫描
            while (iterator.hasNext()) {
                String perDict = iterator.next(); // 遍历的每一个字典的字                // 判断context当前行是否包含这个字典
                boolean contains = tempRow.toLowerCase().contains(perDict.trim());                int index = tempRow.indexOf(perDict + "_");                if (contains && index == -1) {                    if (! linkedList.contains(perDict.trim())) {
                        linkedList.add(perDict.trim());
                    }                    if (contains && ! (Boolean) linkedHashMap.get("flag")) {
                        linkedHashMap.put("flag", true);
                    }
                }
            }
        }

        linkedHashMap.put("containTable", linkedList);

        outputBufferedWriter.write(linkedHashMap.toString());

        outputBufferedWriter.flush();

        contextBufferedReader.close();
        dictionaryBufferedReader.close();
        outputBufferedWriter.close();
        logger.info(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(LocalDateTime.now()) + linkedHashMap.toString());
        
    }
}

封装成JAR 用CMD脚本跑程序【Run.cmd】:

java -jar (定义的jar包文件名) xxx.jarpause

 

程序与读取的文件要放在同一个目录下:

【Java】自制工具_Java