您的位置:首页 > 编程语言 > Java开发

java读取properties配置文件

2015-06-30 14:01 561 查看
Properties properties =new Properties();

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

// CASE 2
try{
//loadAllProperties直接使用路径
properties = PropertiesLoaderUtils.loadAllProperties("E:/config.properties");
properties.getProperty(propKey);
}catch(Exception e){
logger.error("An error occured!");
}

// CASE 3
try{
Resource resource = new ClassPathResource("config.properties");
properties =PropertiesLoaderUtils.loadProperties(resource);
value = properties.getProperty(propKey);
}catch(Exception e){
logger.error("An error occured!");
}

// CASE 4
try{
Resource resource = new ClassPathResource("config.properties");
PropertiesLoaderUtils.fillProperties(properties, resource);
value = properties.getProperty(propKey);
}catch(Exception e){
logger.error("An error occured!");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: