java读取配置文件工具类

来源:互联网 发布:生活 知乎 编辑:程序博客网 时间:2024/04/30 05:01
最近在维护JAVASE的程序代码,需要将原来写死在程序里面的参数移到配置文件里面,
写了一个获取Properties文件的工具类,
代码1:

public class PropUtil {public static Properties getPropUtil () {Properties config = new Properties();InputStream is = null;try {is = PropUtil .class.getClassLoader().getResourceAsStream("prop.properties");config.load(is);} catch (IOException e) {} finally {//关闭资源if (is != null) {try {is.close();} catch (IOException e) {}}}log.debug("return prop config");return config;}}

代码2:
public class PropUtil {private static ResourceBundle resources;public static String getParam(String confFileName,String param) {resources = ResourceBundle.getBundle(confFileName);return resources.getString(param);}}

代码3:
public static String getParameter(String key){Properties params = new  Properties();FileInputStream in = null;String value =null;try{in = new FileInputStream("prop.properties");params.load(in);value = params.getProperty(key);} catch (java.io.IOException e){e.printStackTrace();}finally {if(in!=null) {try {                    in.close();                } catch (IOException e) {                e.printStackTrace();                }}}return value;}

我的是一个纯JAVASE的工程,prop.properties文件放在SRC目录下,代码1和代码2可以正常找到配置项,代码3总是提示资源文件不存在.
问题一:不知道JAVASE中默认的根路径是在哪里呢?不是bin目录下吗
问题二:这三种方式哪个更好呢?
原创粉丝点击