码迷,mamicode.com
首页 > 编程语言 > 详细

Java中读取properties 文件

时间:2015-07-08 01:58:00      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

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!");
}

 

Java中读取properties 文件

标签:

原文地址:http://www.cnblogs.com/chenhao1990/p/4628964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!