溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請您在轉(zhuǎn)載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請您在轉(zhuǎn)載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
          package com.future.util;

          import java.io.FileNotFoundException;
          import java.io.IOException;
          import java.io.InputStream;
          import java.util.Properties;

          /**
           * 讀取資源配置文件
           * 
          @author coder
           *
           
          */

          @SuppressWarnings(
          "serial")
          public class CommonPropertiesUtil {

              @SuppressWarnings(
          "static-access")
              
          public static String getContextPath(){
                  String contextPath
          ="";
                  
          try {
                      
                      
          //加載src目錄下的文件的幾種方式
                      
                      
          //注意當(dāng)使用getClass()方式而不是getClassLoader()時資源文件前的"/"不能省略
                      
                      
          //InputStream stream=this.getClass().getResourceAsStream("/common.properties");
                      
                      
          //InputStream stream=Thread.currentThread().getClass().getResourceAsStream("/common.properties");
                      
                      InputStream stream
          =Thread.currentThread().getContextClassLoader().getResourceAsStream("common.properties");
                      
                      
          //謹(jǐn)記這種方式是錯誤的
                      
          //InputStream stream=Thread.currentThread().getClass().getClassLoader().getResourceAsStream("common.properties");
                      
                      
          //InputStream stream=Thread.currentThread().getClass().getClassLoader().getSystemResourceAsStream("common.properties");
                      
                      
          //InputStream stream=this.getClass().getClassLoader().getSystemResourceAsStream("common.properties");
                      
                      
          //InputStream stream=this.getClass().getClassLoader().getResourceAsStream("common.properties");
                      
                      
          //InputStream stream=this.getClass().getClassLoader().getSystemResourceAsStream("common.properties");
                      
                      
          //InputStream stream=CommonPropertiesUtil.class.getClassLoader().getResourceAsStream("common.properties");
                      
                      
          //InputStream stream=CommonPropertiesUtil.class.getClass().getClassLoader().getSystemClassLoader().getResourceAsStream("common.properties");
                      
                      
          //InputStream stream=CommonPropertiesUtil.class.getClass().getClassLoader().getSystemClassLoader().getSystemResourceAsStream("common.properties");
                      
                      Properties properties
          =new Properties();
                      properties.load(stream);
                      
                      contextPath
          =properties.getProperty("contextPath");
                      
                      System.out.println(contextPath);
                  }
           catch (FileNotFoundException e) {
                      e.printStackTrace();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }

                  
                  
          return contextPath;
              }
           
              
              
          public static void main(String[] args) {
                  getContextPath();
              }

              
          }


                  注:當(dāng)直接使用getClass()而不是getClassLoader()方法時括號中資源文件名稱前的/不能省略
                  

          Feedback

          # re: Java加載src目錄下文件的幾種方式,談?wù)勀愕囊娊?a name="Post"> 2010-08-02 13:21 Mercy
          博主的方法也又一點小小的問題,

          如果執(zhí)行并且沒有限制RuntimePermission("setContextClassLoader")的話:
          Thread.currentThread().setContextClassLoader(null);

          那么NullPointerException就會被raised.

          所以,還要利用
          ClassLoader.getSystemClassLoader()來彌補這個問題。  回復(fù)  更多評論
            

          主站蜘蛛池模板: 沂南县| 甘南县| 大兴区| 萝北县| 苏尼特左旗| 璧山县| 崇明县| 株洲县| 贡觉县| 波密县| 康乐县| 林芝县| 平昌县| 恩平市| 海南省| 合江县| 绵阳市| 界首市| 兰西县| 灵寿县| 贵定县| 驻马店市| 宣城市| 兰坪| 云浮市| 鞍山市| 福贡县| 文成县| 定西市| 南阳市| 台中市| 内乡县| 武山县| 通许县| 贡山| 温宿县| 阜新市| 建德市| 安阳市| 德兴市| 罗田县|