java properties文件操作

来源:互联网 发布:金山毒霸网络卸载密码 编辑:程序博客网 时间:2024/05/29 08:34
package com.weike.platform.util;


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class BankErrorCodeUtil {


private static Properties properties=null;

static{
if(null==properties){
properties = new Properties();
InputStream in = null;
try {
in = BankErrorCodeUtil.class.getClassLoader().getResourceAsStream("bank-error-code.properties");
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* 添加错误编码
* @param key
* @param value
* @return
*/
public static boolean addErrorCode(String key,String value){
boolean flag = false;
try{
properties.put(key, value);
flag = true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}

/**
* 验证错误编码
* @param key
* @return
*/
public static boolean checkErrorCode(String key){
return properties.containsKey(key);
}

/**
* 获取错误信息
* @param key
* @return
*/
public static String getErrorText(String key){
return properties.get(key).toString();
}

}