读取资源文件的方法

1:用Properties类
File confFile = new File("资源文件名");
Properties prpt = new Properties();
try {
 FileInputStream in = new FileInputStream(confFile);
 prpt.load(in);
} catch (FileNotFoundException e) {
 out.print(e);
} catch (IOException e) {
 out.print(e);
} catch (Exception e) {
 out.print(e);
}

prpt.getProperty("xxxx");

2.用ResourceBundle类
// test为没有扩展名的配置文件名,实际文件为test.properties,
// 要放在包根目录及其子目录下
ResourceBundle rb = ResourceBundle.getBundle("test");
rb.getString("xxx")

3.开源工具

网上很多,略。

ps:

获取配置文件目录的方法:
1,jsp程序中:
 request.getRealPath("/xxx.properties")
2,应用程序中:
 URL url = 类名.class.getResource("/xxx.properties");
 try {
  new FileInputStream(url.getFile());
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 }

posted on 2010-04-02 16:42  HelloWorld!  阅读(309)  评论(0)    收藏  举报

导航