zoukankan      html  css  js  c++  java
  • Java中读取properties 文件

    Properties properties = new Properties();
    
    // 方法1
    try {
        // 在加载的class文件中加载,文件是和类文件放在一下的
        ClassLoader loader = PropertiesUtil.class.getClassLoader();
        InputStream inStream = loader.getResourceAsStream("config.properties");
        properties.load(inStream);
        value = properties.getProperty(propKey);
    } catch (Exception e) {
        logger.error("An error occured!");
    }
    
    // 方法2
    try {
        // loadAllProperties直接使用路径
        properties = PropertiesLoaderUtils
                .loadAllProperties("E:/config.properties");
        properties.getProperty(propKey);
    } catch (Exception e) {
        logger.error("An error occured!");
    }
    
    // 方法3
    try {
        Resource resource = new ClassPathResource("config.properties");
        properties = PropertiesLoaderUtils.loadProperties(resource);
        value = properties.getProperty(propKey);
    } catch (Exception e) {
        logger.error("An error occured!");
    }
    
    // 方法4
    try {
        Resource resource = new ClassPathResource("config.properties");
        PropertiesLoaderUtils.fillProperties(properties, resource);
        value = properties.getProperty(propKey);
    } catch (Exception e) {
        logger.error("An error occured!");
    }
  • 相关阅读:
    Hibernate初级
    Servlet, Listener 、 Filter.
    DBCP数据源
    数据库连接池
    MySQL入门笔记
    20170330 webservice代理类测试
    20170330 ABAP代理生成
    20170329 隐士增强问题
    ABAP rfc 发布webservice 错误
    ABAP 性能优化001
  • 原文地址:https://www.cnblogs.com/chenhao1990/p/4628964.html
Copyright © 2011-2022 走看看