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技術
          主站蜘蛛池模板: 法库县| 昂仁县| 海伦市| 泸水县| 喀喇| 安康市| 梁山县| 克拉玛依市| 大洼县| 天镇县| 大田县| 永昌县| 新建县| 昆明市| 临潭县| 平顺县| 丹东市| 额尔古纳市| 修文县| 习水县| 新和县| 天镇县| 峨边| 峨眉山市| 社旗县| 甘南县| 保定市| 芮城县| 大同县| 永仁县| 邵阳市| 铜山县| 乌什县| 隆安县| 磴口县| 高雄县| 高邮市| 四会市| 竹北市| 灌云县| 大丰市|