Java解析Property文件

在Java項目中一些配置參數保存在Property文件中,這樣能保證不修改原代碼直接修改Property文件。

PropertyParser.java

package com.discover.parse;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author Administrator
 *
 */
public class PropertyParser {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Properties properties = new Properties();
        String name = PropertyParser.class.getResource("").getPath();
        String path = name + "config.properties";
        File file = new File(path);
        if(file.exists())
        {
            try{
                InputStream fis = new FileInputStream(file);
                properties.load(fis);
                System.out.println(properties.getProperty("ip"));
            fis.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }
}

config.properties

ip=127.0.0.1

運行程序,控制檯打印出結果



發佈了94 篇原創文章 · 獲贊 102 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章