[size=medium]最近在维护JAVASE的程序代码,需要将原来写死在程序里面的参数移到配置文件里面,
写了一个获取Properties文件的工具类,
代码1:
[/size]
[size=medium]代码2:[/size]
[size=medium]代码3:[/size]
[size=medium]我的是一个纯JAVASE的工程,prop.properties文件放在SRC目录下,代码1和代码2可以正常找到配置项,代码3总是提示资源文件不存在.
问题一:不知道JAVASE中默认的根路径是在哪里呢?不是bin目录下吗
问题二:这三种方式哪个更好呢?[/size]
写了一个获取Properties文件的工具类,
代码1:
[/size]
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;
}
}
[size=medium]代码2:[/size]
public class PropUtil {
private static ResourceBundle resources;
public static String getParam(String confFileName,String param) {
resources = ResourceBundle.getBundle(confFileName);
return resources.getString(param);
}
}
[size=medium]代码3:[/size]
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;
}
[size=medium]我的是一个纯JAVASE的工程,prop.properties文件放在SRC目录下,代码1和代码2可以正常找到配置项,代码3总是提示资源文件不存在.
问题一:不知道JAVASE中默认的根路径是在哪里呢?不是bin目录下吗
问题二:这三种方式哪个更好呢?[/size]