posts - 22, comments - 17, trackbacks - 0, articles - 15
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          讀取properties的幾種方法

          Posted on 2006-12-02 16:16 碼農(nóng)cz 閱讀(312) 評(píng)論(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("沒(méi)有發(fā)現(xiàn)所需要的配置文件!"+e);
          ?9????e.printStackTrace();
          10???}?catch?(IOException?e)?{
          11????//logger.info("讀取文件的過(guò)程中出錯(cuò)!"+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下,不同的操作系統(tǒng)需要使用File.pathSeparatorChar進(jìn)行分割
          15?

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 陈巴尔虎旗| 泊头市| 邵武市| 建宁县| 灯塔市| 简阳市| 皋兰县| 克拉玛依市| 精河县| 满洲里市| 蒙山县| 中山市| 宜州市| 张家界市| 高台县| 崇州市| 北流市| 丰县| 壤塘县| 平湖市| 清河县| 和政县| 东乡族自治县| 大足县| 平定县| 贞丰县| 邳州市| 遵义县| 磴口县| 沅江市| 北川| 光泽县| 毕节市| 精河县| 扎赉特旗| 闵行区| 休宁县| 庐江县| 昌邑市| 宜城市| 民权县|