java读取配置文件工具类

本文讨论了在JavaSE工程中将固定参数移至配置文件Properties的三种方法,并对比了它们的优劣,旨在提供有效的参数管理策略。
最近在维护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目录下吗
问题二:这三种方式哪个更好呢?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值