• 【Java】自制查找工具


    需求:查找后台代码中写的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();
            // 封装结构
            List<String> dictList = Arrays.asList(dictString.split(","));
    
            // 扫描的context行
            String tempRow = "";
    
            LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap();
            linkedHashMap.put("flag", false);
    
            LinkedList linkedList = new LinkedList();
    
            // 对context进行扫描
            while (null != (tempRow = contextBufferedReader.readLine())) {
                // 创建字典的迭代器
                Iterator<String> iterator = 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.jar
    pause

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

  • 相关阅读:
    bcftools 为 vcf 文件建索引及合并 vcf 文件
    Linux 替换^M字符方法
    shell 字符串分割方法简介
    shell 数组介绍及相关操作
    Annovar 信息注释
    C++ string与数值的转换
    C/C++ 删除文件 remove函数
    关于内核转储(core dump)的设置方法
    mac下nginx安装
    linux独有的sendfile系统调用--“零拷贝,高效”
  • 原文地址:https://www.cnblogs.com/mindzone/p/14758058.html
Copyright © 2020-2023  润新知