zoukankan      html  css  js  c++  java
  • 读取指定路径中的明文配置文件

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

    /**
     * 应用场景:某个路径下,有一个文件,其内容为:
     * 第一行是内容的标题,我们不关心
     * 从第二行开始。。。
     * 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();
        }
    }
     
  • 相关阅读:
    定义和使用EL函数
    在Java Web程序中使用Hibernate
    JDBC在Java Web中的应用——分页查询
    JDBC调用存储过程
    使用navicat工具创建MySQL存储过程
    JDBC操作数据库的批处理
    JDBC操作数据库
    Servlet监听器统计在线人数
    Servlet字符编码过滤器
    Servlet过滤器创建与配置
  • 原文地址:https://www.cnblogs.com/Night-Watch/p/12675037.html
Copyright © 2011-2022 走看看