1. 程式人生 > >關於properties配置檔案讀寫,追加以及中文亂碼問題

關於properties配置檔案讀寫,追加以及中文亂碼問題

在開發中常用properties檔案來儲存系統配置資訊,下面就properties檔案的讀寫,資訊追加作簡要介紹,順便也解決亂碼問題。
1、首先介紹一下properties類
properties類繼承自Hashtable

package com.gmi.client.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import
java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Properties; /** * @author xiaoxin.tang * */ public class
PropUtil {
public static String readProperty(String file, String key) { InputStream is = null; FileInputStream fis = null; Properties prop = null; try { prop = new Properties(); fis = new FileInputStream(file); is = new BufferedInputStream(fis); prop.load(is); String value = new
String(prop.getProperty(key, "").getBytes("ISO-8859-1"), "UTF-8"); return value; } catch (Exception e) { e.printStackTrace(); return ""; } finally { if (prop != null) prop = null; //fis try { if (fis != null) { fis.close(); fis = null; } } catch (Exception e) { e.printStackTrace(); } //is try { if (is != null) { is.close(); is = null; } } catch (Exception e) { e.printStackTrace(); } } } public static String readLocalProperty(String key) { String path = ""; try { path = (PropUtil.class.getClassLoader().getResource("").toURI()).getPath(); } catch (URISyntaxException e) { e.printStackTrace(); } return readProperty(path + "conf.properties", key); } public static void writeProperty(String filename,String key,String value){ FileInputStream fis = null; Properties properties = new Properties(); try { fis = new FileInputStream(filename); BufferedReader bf = new BufferedReader(new InputStreamReader(fis,"UTF-8")); properties.load(bf); OutputStream out = new FileOutputStream(filename); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out,"UTF-8")); properties.setProperty(key, value); properties.store(bw, "propertylist"); out.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void writeLocalProperty(String key,String value){ String path = ""; try { path = (PropUtil.class.getClassLoader().getResource("").toURI()).getPath(); } catch (URISyntaxException e) { e.printStackTrace(); } writeProperty(path+"conf.properties", key, value); } public static List<Integer> parseNum2List(String numStr) { List<Integer> list = new ArrayList<Integer>(); StringBuilder sb = null; int len = numStr.length(); for (int i = 0;i < len;i++) { char ch = numStr.charAt(i); if (Character.isDigit(ch)) { if(null == sb){ sb = new StringBuilder(); } sb.append(ch); } else { if (null != sb) { list.add(Integer.parseInt(sb.toString())); sb = null; } } } // Add the last num if (null != sb) { list.add(Integer.parseInt(sb.toString())); } return list; } }

獲取部署專案的properties檔案路徑:PropUtil.class.getClassLoader().getResource(“”).toURI()).getPath();
配置資訊追加:首先把properties檔案中原有的資訊load出來,然後在store,這樣就不存在清空原資訊的問題了
fis = new FileInputStream(filename);
BufferedReader bf = new BufferedReader(new InputStreamReader(fis,”UTF-8”));
properties.load(bf);
解決中文亂碼問題:由於採用位元組流提取/寫回資訊,開啟properties後發現亂碼問題,把位元組流轉換成字元流,問題就迎刃而解了!