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;

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); 

Java学习交流群 636898482:有问题请及时加群咨询 Java一对一辅导 https://tomcode.taobao.com/
原文地址:https://www.cnblogs.com/jsjjob/p/8196423.html