码迷,mamicode.com
首页 > 编程语言 > 详细

java 读取配置文件工具类 (how to read values from properties file in java)

时间:2017-07-13 16:14:35      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:默认值   put   util   div   values   getc   static   try   private   

Java 读取配置文件工具类

使用 java.util.Properties 

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

public class PropertiesReader {

    private static Properties prop;

    static {
        reload();
    }

    private static void reload() {
        prop = new Properties();

        try {
            InputStream inputStream = PropertiesReader.class.getClassLoader().getResourceAsStream("config.properties");

            prop.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取指定的系统属性值
     *
     * @param key
     * @return
     */
    public static String getProperty(String key) {

        return prop.getProperty(key);
    }

    /**
     * 获取指定的系统属性值(带默认值)
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getProperty(String key, String defaultValue) {

        return prop.getProperty(key, defaultValue);
    }

}

 

java 读取配置文件工具类 (how to read values from properties file in java)

标签:默认值   put   util   div   values   getc   static   try   private   

原文地址:http://www.cnblogs.com/marvinlh/p/7160166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!