关于在类中加载资源文件的问题
读取资源文件类
package SysInfo;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.util.ResourceBundle;
import java.util.MissingResourceException;
import java.util.Locale;
public class Config {
static private Config instance;
private static ResourceBundle resources;
static synchronized public Config getInstance() {
if (instance == null) {
instance = new Config();
}
return instance;
}
private Config(){
init();
}
private static synchronized void init()
{
try {
resources = ResourceBundle.getBundle("SysInfo.app",Locale.getDefault());
/*getBundle(String baseName,Local local)
*baseName:包名+文件名(不需含扩展名及区域标志),app.properties,app_en.properties app_zh_CN.properties
*local.getDefault():取得区域(系统控制面板设置)
*如不存在Locale.getDefault()对应的资源文件,则去取app.properties
*/
} catch (MissingResourceException mre) {
System.err.println("资源文件没有找到");
}
}
private static synchronized boolean checkResources(){
if(resources==null){
Config.log("未能获取服务器配置文件");
return false;
}
return true;
}
public static synchronized String getString(String key){
if(checkResources())
return resources.getString(key);
return null;
}
public static synchronized void log(String str){
System.out.println(str);
}
public static synchronized void main(String[] args) {
System.out.println(getString("SYS.CONSTRUCT.UPLOADPATH"));
}
}
其中app.properties也是在包SysInfo下,另外我也试过不使用包名,但运行后却报
未能获取服务器配置文件
null
我这个是普通项目(jsp+javabean),以前我在struts下使用该加载方法能成功
这是为什么,该如何解决
问题点数:20、回复次数:6Top
1 楼yanxibang(yanxibang(执子之手,与子偕老))回复于 2005-01-11 12:27:12 得分 0
顶Top
2 楼dujianmeng(heihei)回复于 2005-01-11 12:32:00 得分 10
try {
InputStream fis = getClass().getResourceAsStream("SysInfo.app");
prop = new Properties();
try {
prop.load(fis);
//System.out.println("load SysInfo.app success");
fis.close();
}
catch ( java.io.IOException exc ) {
exc.printStackTrace();
}
}
catch (Exception exc ) {
System.out.println("Can't find config file!");
exc.printStackTrace();
}
试试这个Top
3 楼jiangx123(jiangx123)回复于 2005-01-11 12:43:36 得分 10
干吗要同步?Top
4 楼yanxibang(yanxibang(执子之手,与子偕老))回复于 2005-01-11 13:21:40 得分 0
用Properties估计问题不大,不过单纯作为资源文件的话还是用ResourceBundle类比较好,便于国际化及
它是只读的(安全性高一些)
Top
5 楼yanxibang(yanxibang(执子之手,与子偕老))回复于 2005-01-11 15:39:15 得分 0
为何大家不帮帮我啊,Top
6 楼yanxibang(yanxibang(执子之手,与子偕老))回复于 2005-01-12 09:01:34 得分 0
再up一下,Top




