1、获取属性资源文件

 public static String getPropPath(){
  String path = "";
     try {
       URL url = ContextUtil.class.getProtectionDomain().getCodeSource().getLocation();
       path = URLDecoder.decode(url.getPath(), "utf-8");
       if (path.contains("classes/"))
         path = path.substring(1, path.indexOf("classes"));
       else {
         path = path.substring(1, path.indexOf("lib"));
       }
       path += "jdbc.properties";
     } catch (IOException e) {
       logger.error("属性文件路径错误!" + getTrace(e));
     }
     return path;
 }

2、获取属性

public static Properties getProperty(){
  Properties p = null;
        try{
         p=readPropertiesFileObj(getPropPath());
        }catch(Exception e){
         logger.error("jdbc.properties 文件获取数据:"+getPropPath()+ContextUtil.getTrace(e));
        }
        return p;

 }

3、更新属性文件
 public static void updateProperties(String keyname, String keyvalue)
 {
     Properties props = null;
     props = getProperty();
     props.setProperty(keyname, keyvalue);
     writePropertiesFileObj(getPropPath(),props);
 }
 /*
  *  读取资源文件,并处理中文乱码
  */
    public static Properties readPropertiesFileObj(String filename) {
        Properties properties = new OrderedProperties();
        try {
            InputStream inputStream = new FileInputStream(filename);
            BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            properties.load(bf);
            inputStream.close(); // 关闭流
        } catch (IOException e) {
         logger.error("属性文件更新错误!readPropertiesFileObj" + getTrace(e));
        }
        return properties;
    }
    /*
     * 写资源文件,含中文并保持原始键值对的顺序
     */
    public static void writePropertiesFileObj(String filename, Properties properties) {
        if (properties == null) {
            properties = new OrderedProperties();
        }
        try {
            OutputStream outputStream = new FileOutputStream(filename);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
            properties.store(bw, null);
            outputStream.close();
        } catch (IOException e) {
         logger.error("属性文件更新错误writePropertiesFileObj!" + getTrace(e));
        }
    }

 4、删除指定属性资源文件的键值对

public static boolean deleteKeyValuePro(String delete_key, String delete_value, String filePath) {
  boolean flag = false;
  String toreplace =delete_key + "=" + delete_value + "\\r\\n";
  try {
   StringBuffer sb = new StringBuffer();
   String templine;
   File file = new File(filePath);
   BufferedReader bin = new BufferedReader(new FileReader(file));
   while ((templine = bin.readLine()) != null) {
    templine = unicodeToString(templine);
    sb.append(templine + "\\r\\n");
   }

    //处理转义字符串
   String save =StringEscapeUtils.unescapeJava(sb.toString());   Pattern pattern = Pattern.compile(toreplace, Pattern.MULTILINE);
   Matcher matcher = pattern.matcher(save);
   while (matcher.find()) {
    save = matcher.replaceAll("");
   }
   FileOutputStream output=new FileOutputStream(file);
   byte [] buff=new byte[]{};
            buff=save.getBytes();
            output.write(buff, 0, buff.length);
            output.close();
   flag = true;
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return flag;
 }

OrderedProperties类代码如下:

import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Properties;
import java.util.Set;
/*
 * 顺序 读写 Properties 配置文件 支持中文 不乱码
 */
public class OrderedProperties extends Properties {
    private static final long serialVersionUID = -4627607243846121965L;
    private final LinkedHashSetkeys = new LinkedHashSet();

    public Enumerationkeys() {
        return Collections.enumeration(keys);
    }

    public Object put(Object key, Object value) {
        keys.add(key);
        return super.put(key, value);
    }
   
    public synchronized Object remove(Object key) {
        keys.remove(key);
        return super.remove(key);
    }

    public SetkeySet() {
        return keys;
    }

    public SetstringPropertyNames() {
        Setset = new LinkedHashSet();
        for (Object key : this.keys) {
            set.add((String) key);
        }
        return set;

    }
}

 


注意:本文归作者所有,未经作者允许,不得转载