關於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後發現亂碼問題,把字節流轉換成字符流,問題就迎刃而解了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章