posts - 60,comments - 71,trackbacks - 0

              JAVA中用到資源文件的情況非常多,所以開發中經常會遇到讀取.properties文件的情況,現作些總結:

          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類的構造函數
          示例:
          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()靜態方法
          示例:
          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當前線程方法獲取
          示例:
          Properties properties = new Properties();
          properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(path));

          posted on 2008-05-27 16:06 henry1451 閱讀(323) 評論(0)  編輯  收藏 所屬分類: Java技術
          主站蜘蛛池模板: 同江市| 海盐县| 内乡县| 靖边县| 石城县| 阿尔山市| 石河子市| 三穗县| 乌兰察布市| 上杭县| 垫江县| 赤水市| 遂宁市| 乐都县| 札达县| 称多县| 达州市| 驻马店市| 龙山县| 界首市| 汝阳县| 商都县| 通渭县| 通城县| 商河县| 海晏县| 高安市| 卓尼县| 镇平县| 乳源| 长兴县| 旬邑县| 阜城县| 炎陵县| 子洲县| 梅河口市| 东明县| 苍梧县| 鲁甸县| 囊谦县| 永德县|