Java解析Property文件

来源:互联网 发布:淘宝网男加宽牛仔裤 编辑:程序博客网 时间:2024/06/11 06:00

在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

运行程序,控制台打印出结果



2 0
原创粉丝点击