java讀取自定義的.properties 配置文件 中的key-value值


  /**
     * 讀取 .properties 配置文件 
     * @param propertiesUrl 配置文件的路徑
     * @return 配置文件中的key-value值
     */
    public static Map<String, String> getProperties(String propertiesUrl) {
        Map<String, String> resultMap = new HashMap<String, String>();
        Properties prop = new Properties();
        try {
            // 讀取屬性文件a.properties
            InputStream in = new BufferedInputStream(new FileInputStream(propertiesUrl));
            // 加載屬性列表
            prop.load(in);
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                String key = it.next();
                resultMap.put(key, prop.getProperty(key));
                System.out.println("key: "+key+",      value: "+prop.getProperty(key));
            }
            in.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        return resultMap;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM