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技術
          主站蜘蛛池模板: 雷州市| 嘉祥县| 喜德县| 中西区| 石嘴山市| 泾阳县| 厦门市| 高阳县| 宁陵县| 黎平县| 台南县| 三门县| 重庆市| 通渭县| 景泰县| 盐池县| 织金县| 通江县| 忻城县| 通许县| 信丰县| 土默特右旗| 济阳县| 泊头市| 罗甸县| 德安县| 苏州市| 岳池县| 禹城市| 巴林右旗| 庆城县| 漠河县| 襄汾县| 彭州市| 同心县| 虹口区| 孟津县| 莒南县| 隆安县| 梁河县| 清涧县|