且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java Properties工具类,包括新增、更新及写入文件【解决中文乱码问题】

更新时间:2021-12-23 02:34:14

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.Properties;

public class PropertiesUtil {

    public static final Properties properties = new Properties();
    public static final String path = "config.properties";

    public static void init() {
        InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(path);
        try {
            properties.load(new InputStreamReader(inputStream,"UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *   获取指定的value值
     * @param key 要删除的属性key
     * @author liguangni
     */
    public static String get(String key) {
        return properties.getProperty(key);
    }

    /**
     *  新增或者修改配置文件(如果指定的key存在,则修改,否则执行新增)
     * @param key 要删除的属性key
     * @author liguangni
     */
    public static void update(String key, String value) {
        properties.setProperty(key, value);
        try(
            OutputStreamWriter     output = new OutputStreamWriter(new FileOutputStream(Thread.currentThread().getContextClassLoader().getResource(path).getPath()), "UTF-8");
        ) {
            properties.store(output, "");
            output.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  删除指定的属性值
     * @param key 要删除的属性key
     * @author liguangni
     */
    public static void delete(String key) {
        properties.remove(key);
        try(
            FileOutputStream oFile = new FileOutputStream(path);
        ) {
            properties.store(oFile, "");
            oFile.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *   获取所有的键值对
     * @param key 要删除的属性key
     * @author liguangni
     */
    public static void list() {
        Enumeration en = properties.propertyNames(); // 得到配置文件的名字
        while (en.hasMoreElements()) {
            String strKey = (String) en.nextElement();
            String strValue = properties.getProperty(strKey);
            System.out.println(strKey + "=" + strValue);
        }
    }
}