java讀寫自定義property文件

import java.io.*;
import java.net.URI;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

public class PropertiesUtils {
    private static Properties properties;
    private URI uri;

    /**
     * 加載屬性文件
     *
     * @param filePath 文件路徑
     * @return
     */
    public static Properties loadProps(String filePath) {
        properties = new Properties();
        String path = null;
        try {
            URL xmlpath = PropertiesUtils.class.getClassLoader().getResource(filePath);
            if (xmlpath != null) {
                path = xmlpath.getPath();
//                InputStream in = new BufferedInputStream(new FileInputStream(path));
                InputStream in = PropertiesUtils.class.getClassLoader().getResourceAsStream(filePath);
                properties.load(in);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return properties;
    }

    /**
     * 讀取配置文件
     *
     * @param
     * @param key
     * @return
     */
    public static String getString(Properties properties, String key) {
        return properties.getProperty(key);
    }

    /**
     * 更新配置文件
     *
     * @return
     */
    public static void updatePropertyLoginError(Properties properties, String filePath, String keyname, String keyvalue) {
        try {
            properties.setProperty(keyname, keyvalue);
            String path = PropertiesUtils.class.getClassLoader().getResource(filePath).getPath();
            FileOutputStream outputFile = new FileOutputStream(path);
            properties.store(outputFile, "modify");
            outputFile.flush();
            outputFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     *
     * @param filePath
     * @param key
     * @return
     */
    public static String GetValueByKey(String filePath, String key) {
        Properties pps = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            pps.load(in);
            String value = pps.getProperty(key);
            System.out.println(key + " = " + value);
            return value;

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    //讀取Properties的全部信息
    /**
     *
     * @throws IOException
     */
    public static void GetAllProperties() throws IOException {
        Properties pps = new Properties();
        String filePath = null;
        URL xmlpath = PropertiesUtils.class.getClassLoader().getResource("raspberry.properties");
        System.out.println(xmlpath);
        if (xmlpath != null) {
            filePath = xmlpath.getPath();
        }
        InputStream in = new BufferedInputStream(new FileInputStream(filePath));
        pps.load(in);
        Enumeration en = pps.propertyNames(); //得到配置文件的名字

        while (en.hasMoreElements()) {
            String strKey = (String) en.nextElement();
            String strValue = pps.getProperty(strKey);
            System.out.println(strKey + "=" + strValue);
        }

    }

    /**
     *
     * @param filePath
     * @param pKey
     * @param pValue
     * @return
     * @throws IOException
     */
    public static boolean WriteProperties(String filePath, String pKey, String pValue) throws IOException {
        Properties pps = new Properties();
        String path = null;
        URL xmlpath = PropertiesUtils.class.getClassLoader().getResource(filePath);

        if(null!=xmlpath){
            path = xmlpath.getPath();
            InputStream in = new FileInputStream(path);
            //從輸入流中讀取屬性列表(鍵和元素對)
            pps.load(in);
            //調用 Hashtable 的方法 put。使用 getProperty 方法提供並行性。
            //強制要求爲屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。
            OutputStream out = new FileOutputStream(path);
            pps.setProperty(pKey, pValue);
            //以適合使用 load 方法加載到 Properties 表中的格式,
            //將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
            pps.store(out, "Update" + pKey + "name");
            out.flush();
            out.close();
            return true;
        }else{
            return false;
        }

    }

    /**
     *
     * @param properties
     * @param filePath
     * @param keyname
     * @param keyvalue
     */
    public static void updateProperty(Properties properties,String filePath,String keyname,String keyvalue) {
        try {

            // 從輸入流中讀取屬性列表(鍵和元素對)
            properties.setProperty(keyname, keyvalue);
            FileOutputStream outputFile = new FileOutputStream(filePath);
            properties.store(outputFile, null);
            outputFile.flush();
            outputFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*public static void main(String[] args) throws IOException {

        URL xmlpath = PropertiesUtils.class.getClassLoader().getResource("raspberry.properties");
        System.out.println(xmlpath);
        properties = loadProps(xmlpath.getPath());
//        updateProperty(properties,"raspberry.properties","password","wwwwwwww");

//        GetAllProperties();
        WriteProperties("raspberry.properties","userName","microlink");
    }*/


}

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