读取指定路径中的明文配置文件

读取指定路径中的明文配置文件

/**
 * 应用场景:某个路径下,有一个文件,其内容为:
 * 第一行是内容的标题,我们不关心
 * 从第二行开始。。。
 * com.driverClass=abc
 * userName=wxp
 * ....
 * 我们读取后,经过处理,打印出来的就是
 * driverClass = abc
 * userName=wxp
 * 也就是split[0] = split[1] 
 * @param filePath
 */
@SuppressWarnings("resource")
public static void readTextFile(String filePath){
    try {
        String encoding = "GBK";
        File file = new File(filePath);
        if (file.isFile() && file.exists()) {
            InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            int line = 0;    
            while ((lineTxt = bufferedReader.readLine()) != null) {
                line ++;
                if (line == 1) {    // skip the first line content of the file
                    continue;
                }
                String[] split = lineTxt.split("=");
                if (split[0].contains(".")) {
                    split[0] = split[0].substring(split[0].lastIndexOf(".") + 1, split[0].length()).toString();
                }
                System.out.println(split[0] + "=" + split[1]);
                
            }
            read.close();
        }else {
            System.out.println("This is not a file or the file doesn't exist.");
        }
    } catch (Exception e) {
        System.out.println("Failed to read the file.");
        e.printStackTrace();
    }
}
 
原文地址:https://www.cnblogs.com/Night-Watch/p/12675037.html