posts - 60,comments - 71,trackbacks - 0

              JAVA中用到資源文件的情況非常多,所以開發(fā)中經(jīng)常會遇到讀取.properties文件的情況,現(xiàn)作些總結(jié):

          1.使用java.util.Properties類的load()方法
          示例:
          InputStream in = lnew BufferedInputStream(new FileInputStream(name));
          Properties p = new Properties();
          p.load(in);

          2.使用java.util.ResourceBundle類的getBundle()方法
          示例:
          ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

          3.使用java.util.PropertyResourceBundle類的構(gòu)造函數(shù)
          示例:
          InputStream in = new BufferedInputStream(new FileInputStream(name));
          ResourceBundle rb = new PropertyResourceBundle(in);

          4.使用class變量的getResourceAsStream()方法
          示例:
          InputStream in = JProperties.class.getResourceAsStream(name);
          Properties p = new Properties();
          p.load(in);

          5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
          示例:
          InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
          Properties p = new Properties();
          p.load(in);

          6.使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態(tài)方法
          示例:
          InputStream in = ClassLoader.getSystemResourceAsStream(name);
          Properties p = new Properties();
          p.load(in);

          7.Servlet中可以用javax.servlet.ServletContext 的getResourceAsStream()方法

          示例:
          InputStream in = context.getResourceAsStream(path);
          Properties p = new Properties();
          p.load(in);

          8.用Thread當(dāng)前線程方法獲取
          示例:
          Properties properties = new Properties();
          properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(path));

          posted on 2008-05-27 16:06 henry1451 閱讀(319) 評論(0)  編輯  收藏 所屬分類: Java技術(shù)
          主站蜘蛛池模板: 湾仔区| 隆林| 定远县| 嘉黎县| 乌拉特中旗| 大安市| 依兰县| 凭祥市| 宁陕县| 陆河县| 浮梁县| 东阿县| 手机| 阳山县| 山西省| 溆浦县| 浮山县| 竹溪县| 吕梁市| 贞丰县| 抚松县| 海南省| 板桥市| 芜湖县| 吉首市| 澎湖县| 合肥市| 南平市| 崇仁县| 新兴县| 尼勒克县| 故城县| 左云县| 临江市| 塔城市| 阿荣旗| 雷山县| 江永县| 枣强县| 盐津县| 台州市|