Posted on 2006-12-02 16:16
码农cz 阅读(306)
评论(0) 编辑 收藏 所属分类:
OtherJava
1.
1 private void getContextParam(){
2 InputStream is=this.getClass().getClassLoader().getResourceAsStream("clientConfig.properties");
3 Properties prop= new Properties();
4 try {
5 prop.load(is);
6 this.classForName=prop.getProperty("classForName");
7 this.connectStr=prop.getProperty("connectStr");
8 this.userName=prop.getProperty("userName");
9 this.psw=prop.getProperty("psw");
10 } catch (IOException e) {
11 e.printStackTrace();
12 }
13 }
14 //注:只能将clientConfig.properties放入src下
15
16
2.
1 String realName=this.getServletContext().getRealPath("WEB-INF");
2 Properties prop= new Properties();
3 InputStream is=null;
4 try {
5 is = new BufferedInputStream(new FileInputStream(new File(realName+"/clientConfig.properties")));
6 prop.load(is);
7 } catch (FileNotFoundException e) {
8 //logger.info("没有发现所需要的配置文件!"+e);
9 e.printStackTrace();
10 } catch (IOException e) {
11 //logger.info("读取文件的过程中出错!"+e);
12 e.printStackTrace();
13 }
14
15 //注:同样需要定制文件存放的目录
16
17
3.
1 public static String getValue(String key) {
2
3 private static Properties propertie;
4
5 String path = (Configuration.class.getResource("/") + "").substring(6);
6 Configuration rc = new Configuration(path + "//config.properties");
7 if (propertie.containsKey(key)) {
8 String value = propertie.getProperty(key);// 得到某一属性的值
9 return value;
10 } else
11 return "";
12 }
13
14 //注:只适用在windows下,不同的操作系统需要使用File.pathSeparatorChar进行分割
15