首页 > 编程语言 > 详细

Java中读取properties 文件

时间:2015-07-08 01:55:33      阅读:241      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!