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

java读取配置文件(Properties)

2017-09-05 20:48 323 查看
package com.online.college.common.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertiesUtil {

private static Map<String,Properties> propMap = new HashMap<String,Properties>();

/*设置默认的properties文件,方便操作*/
public static final String DEFAULT_PROPERTIES_FILE="application.properties";

public static Object getProperty(String file,String key){
Properties prop = getProperties(file);
if(prop != null && prop.get(key) != null){
return prop.get(key);
}
return null;
}

public static Properties getProperties(String file){
try {
if(propMap.get(file) == null){
Properties prop = new Properties();
prop.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(file));
propMap.put(file,prop);
return prop;
}else{
return propMap.get(file);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

public static void updateProperties(Properties prop,String filePath){

FileInputStream fis = null;
BufferedInputStream bis = null;
try {
URI fileUri = PropertiesUtil.class.getClassLoader().getResource(filePath).toURI();
File file = new File(fileUri);

Properties tmpProp = new Properties();
fis = new FileInputStream(file);
bis = n
4000
ew BufferedInputStream(fis);
tmpProp.load(bis);

FileOutputStream fos = new FileOutputStream(file);
for(Object key : prop.keySet()){
tmpProp.setProperty(String.valueOf(key),String.valueOf(prop.get(key)));
}
tmpProp.store(fos, null);
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 从默认配置文件中获取properties
* @return
*/
public static Properties getDefaultProperties(){
return getProperties(DEFAULT_PROPERTIES_FILE);
}

/**
* 从默认配置文件中获取配置项
* @param key
* @return
*/
public static String getProperty(String key){
Properties prop = getDefaultProperties();
if(prop != null && prop.get(key) != null){
return prop.getProperty(key);
}
return null;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: