Java property配置文件操作

操作代码如下

package com.base.property;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class PropertiesUtil {

    private static Properties prop = new Properties();

    public static void readProperties(String fileName){
        try {
            InputStream in = PropertiesUtil.class.getResourceAsStream(fileName);
            BufferedReader bf = new BufferedReader(new InputStreamReader(in));
            prop.load(bf);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static String getProperty(String key){
        return prop.getProperty(key);
    }

    public static void main(String[] args) {
        PropertiesUtil.readProperties("env.properties");
        System.out.println(PropertiesUtil.getProperty("App"));
        System.out.println(PropertiesUtil.getProperty("Oriente-Agent"));
    }
}

注:
在本例中PropertiesUtil类的同一个目录下,新建env.properties文件,
文件中包含两个配置项App和Oriente-Agent

App=bestfei
Oriente-Agent={"version":"1.0.2.0","os":"6.0","platform":"android"}

如需修改读取配置文件的地址,可修改PropertiesUtil.readProperties,例如

InputStream in = PropertiesUtil.class.getResourceAsStream("/"+fileName);

你可能感兴趣的:(Java property配置文件操作)