java读取properties文件工具类

来源:互联网 发布:数据建模培训班 编辑:程序博客网 时间:2024/06/06 01:01

代码:

package com.gusy.common.utils;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Properties;public class ConfigUtil {private static Properties pro;static {// path// 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。InputStream input = ConfigUtil.class.getResourceAsStream("/config.properties");pro = new Properties();try {pro.load(input);pro.load(new InputStreamReader(input, "UTF-8"));input.close();} catch (IOException e) {e.printStackTrace();}}// 根据key获取valuepublic static String getValue(String key) {return pro.getProperty(key);}// 调用public static void main(String[] args) {String value = ConfigUtil.getValue("appID");System.out.println(value);}}

附件:config.properties在项目中地址


拓展:

1、java Class类中getResourceAsStream的用法:参考链接:点击打开链接
2、类Proerties中的方法(参考):


0 0
原创粉丝点击