当前位置 : 主页 > 编程语言 > c++ >

Properties操作

来源:互联网 收集:自由互联 发布时间:2021-06-30
读取resources目录下的properties文件为properties对象 public static Properties loadProps(String fileName) { Properties props = null; InputStream is = null; try { is = Thread.currentThread().getContextClassLoader().getResourceAsStre
读取resources目录下的properties文件为properties对象
public static Properties loadProps(String fileName) {
        Properties props = null;
        InputStream is = null;

        try {
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            if (is == null) {
                throw new ClassNotFoundException(fileName + " file is not found");
            }
            props = new Properties();
            props.load(is);
        } catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null){
                try {
                    is.close();
                }catch (IOException e){
                    LOGGER.error("close input stream failure", e);
                }
            }
        }
        return props;
    }
网友评论