posts - 22, comments - 17, trackbacks - 0, articles - 15
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          讀取properties的幾種方法

          Posted on 2006-12-02 16:16 碼農cz 閱讀(311) 評論(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?

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 陆丰市| 泰来县| 运城市| 托克逊县| 和田市| 红安县| 志丹县| 化隆| 高密市| 江津市| 乌恰县| 平陆县| 新竹市| 鸡西市| 花莲市| 金塔县| 敦煌市| 久治县| 新源县| 壤塘县| 内乡县| 页游| 通渭县| 栾川县| 车险| 自治县| 故城县| 白玉县| 宜兰市| 全南县| 岳池县| 西峡县| 白水县| 体育| 双鸭山市| 青州市| 长泰县| 田林县| 延津县| 林州市| 衡山县|