java对.properties配置文件操作
2017-03-21 16:45
323 查看
实现运用Java.util.Properties来进行对.properties配置文件操作。配置文件实例:如debug.properties #Tue Mar 21 15:46:17 CST 2017
#key=value
remote.debug.prot=7451
第一步写个获取文件路径的外部方法
//-in- String filePath:配置文件名如debug.properties--
//-return- 文件类对象--
public static File getFile (String filePath) throws IOException{ try{ ClassLoader classLoader = Thread.currentThread() .getContextClassLoader(); URL url = classLoader.getResource(filePath); return new File(url.toURI()); }catch(Exception e){ LOG.error("配置文件错误",e); return null; } }2.1根据key获取value值方法//-in- String filePath:配置文件名如debug.properties--//-in- key:键值如remote.debug.prot--//-return- value值-- public static String getValueByKey(String filePath, String key) throws IOException{
try {
File fileURI = PropertiesUtil.getFile(filePath);
InputStream in = new BufferedInputStream (new FileInputStream(fileURI));
Properties pps = new Properties();
pps.load(in);
String value = pps.getProperty(key);
return value;
}catch (Exception e) {
LOG.error("初始化配置文件失败",e);
return null;
}
}
2.2得到所有HashMap<key,value>方法//-in- String filePath:配置文件名如debug.properties--//-return- HashMap<String, String>: HashMap<key, value>-- public static HashMap<String, String> getAllProperties(String filePath) throws IOException {
HashMap<String, String> proper = new HashMap<String, String>();
try{
File fileURI = PropertiesUtil.getFile(filePath);
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(fileURI));
pps.load(in);
Enumeration<?> en = pps.propertyNames(); //得到配置文件的名字
while(en.hasMoreElements()) {
String strKey = (String) en.nextElement();
String strValue = pps.getProperty(strKey);
proper.put(strKey, strValue);
}
}catch(Exception e)
{
LOG.error("获取所有配置文件信息失败",e);
return null;
}
return proper;
}
2.3根据key获取value值方法//-in- String filePath:配置文件名如debug.properties--//-in- pKey: 键值如remote.debug.prot--//-in- pValue: 结果值如 2000--//-return- 状态值-- public static String writeProperties (String filePath, String pKey, String pValue) throws IOException {
try{
File fileURI = PropertiesUtil.getFile(filePath);
Properties pps = new Properties();
InputStream in = new FileInputStream(fileURI);
pps.load(in);
OutputStream out = new FileOutputStream(fileURI);
pps.setProperty(pKey, pValue);
pps.store(out, "");
return TRUE;
}catch(Exception e)
{
LOG.error("插入配置文件失败", e);
return null;
}
}
2.3写入所有key,value值方法//-in- String filePath:配置文件名如debug.properties--//-in- HashMap<String, String>: 键值对HashMap<key, value>如remote.debug.prot=900--//-return- 状态值-- public static String setAllProperties(String filePath,HashMap<String, String> kv) throws IOException {
try{
File fileURI = PropertiesUtil.getFile(filePath);
Properties pps = new Properties();
InputStream in = new FileInputStream(fileURI);
pps.load(in);
OutputStream out = new FileOutputStream(fileURI);
Iterator<Entry<String, String>> iter = kv.entrySet().iterator();
while (iter.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
pps.setProperty((String)key, (String)val);
pps.store(out, "");
}
}catch(Exception e)
{
return null;
}
return TRUE;
}
#key=value
remote.debug.prot=7451
第一步写个获取文件路径的外部方法
//-in- String filePath:配置文件名如debug.properties--
//-return- 文件类对象--
public static File getFile (String filePath) throws IOException{ try{ ClassLoader classLoader = Thread.currentThread() .getContextClassLoader(); URL url = classLoader.getResource(filePath); return new File(url.toURI()); }catch(Exception e){ LOG.error("配置文件错误",e); return null; } }2.1根据key获取value值方法//-in- String filePath:配置文件名如debug.properties--//-in- key:键值如remote.debug.prot--//-return- value值-- public static String getValueByKey(String filePath, String key) throws IOException{
try {
File fileURI = PropertiesUtil.getFile(filePath);
InputStream in = new BufferedInputStream (new FileInputStream(fileURI));
Properties pps = new Properties();
pps.load(in);
String value = pps.getProperty(key);
return value;
}catch (Exception e) {
LOG.error("初始化配置文件失败",e);
return null;
}
}
2.2得到所有HashMap<key,value>方法//-in- String filePath:配置文件名如debug.properties--//-return- HashMap<String, String>: HashMap<key, value>-- public static HashMap<String, String> getAllProperties(String filePath) throws IOException {
HashMap<String, String> proper = new HashMap<String, String>();
try{
File fileURI = PropertiesUtil.getFile(filePath);
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(fileURI));
pps.load(in);
Enumeration<?> en = pps.propertyNames(); //得到配置文件的名字
while(en.hasMoreElements()) {
String strKey = (String) en.nextElement();
String strValue = pps.getProperty(strKey);
proper.put(strKey, strValue);
}
}catch(Exception e)
{
LOG.error("获取所有配置文件信息失败",e);
return null;
}
return proper;
}
2.3根据key获取value值方法//-in- String filePath:配置文件名如debug.properties--//-in- pKey: 键值如remote.debug.prot--//-in- pValue: 结果值如 2000--//-return- 状态值-- public static String writeProperties (String filePath, String pKey, String pValue) throws IOException {
try{
File fileURI = PropertiesUtil.getFile(filePath);
Properties pps = new Properties();
InputStream in = new FileInputStream(fileURI);
pps.load(in);
OutputStream out = new FileOutputStream(fileURI);
pps.setProperty(pKey, pValue);
pps.store(out, "");
return TRUE;
}catch(Exception e)
{
LOG.error("插入配置文件失败", e);
return null;
}
}
2.3写入所有key,value值方法//-in- String filePath:配置文件名如debug.properties--//-in- HashMap<String, String>: 键值对HashMap<key, value>如remote.debug.prot=900--//-return- 状态值-- public static String setAllProperties(String filePath,HashMap<String, String> kv) throws IOException {
try{
File fileURI = PropertiesUtil.getFile(filePath);
Properties pps = new Properties();
InputStream in = new FileInputStream(fileURI);
pps.load(in);
OutputStream out = new FileOutputStream(fileURI);
Iterator<Entry<String, String>> iter = kv.entrySet().iterator();
while (iter.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
pps.setProperty((String)key, (String)val);
pps.store(out, "");
}
}catch(Exception e)
{
return null;
}
return TRUE;
}
相关文章推荐
- java操作数据库的类——SqlHelper(读取properties配置文件)
- Java配置文件Properties的读取、写入与更新操作
- java读取Properties文件-操作Properties配置文件
- java中properties配置文件的操作
- Java 操作Properties配置文件详解
- java properties配置文件操作
- Java配置文件Properties的读取、写入与更新操作
- java基础之对properties配置文件的读写操作
- java配合properties配置文件对Excel表格文件的操作
- Java配置文件Properties的读取、写入与更新操作
- JAVA操作properties配置文件——总结(Locale&ResourceBundle& PropertyResourceBundle)
- java对配置文件(XML/properties)的操作
- Java Properties 类读取配置文件信息,Java properties文件的操作(zz)
- Java配置文件Properties的读取、写入与更新操作
- 对Java配置文件Properties的读取、写入与更新操作
- java 操作配置文件 .properties
- Java Properties 类读取配置文件信息,Java properties文件的操作(zz)
- java操作数据库的类——SqlHelper(读取properties配置文件)
- java对properties配置文件的读写操作
- java对properties配置文件的读写操作