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 閱讀(319) 評論(0)  編輯  收藏 所屬分類: Java技術
          主站蜘蛛池模板: 宜阳县| 施甸县| 郑州市| 方城县| 郁南县| 民和| 翁牛特旗| 乌兰浩特市| 普兰县| 甘孜县| 封开县| 建始县| 阳新县| 漠河县| 苍山县| 西藏| 湄潭县| 汨罗市| 铁岭市| 小金县| 蚌埠市| 沙雅县| 江都市| 江华| 新余市| 石阡县| 东乌| 大同市| 东源县| 酉阳| 博客| 行唐县| 泗洪县| 京山县| 石棉县| 修文县| 孟津县| 泌阳县| 宜兴市| 文水县| 同心县|