<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    雪山飛鵠

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

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      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目錄下的文件的幾種方式
                
                
    //注意當使用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");
                
                
    //謹記這種方式是錯誤的
                
    //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();
        }

        
    }


            注:當直接使用getClass()而不是getClassLoader()方法時括號中資源文件名稱前的/不能省略
            
    posted on 2010-08-02 10:11 雪山飛鵠 閱讀(5397) 評論(1)  編輯  收藏 所屬分類: javase

    Feedback

    # re: Java加載src目錄下文件的幾種方式,談談你的見解 2010-08-02 13:21 Mercy
    博主的方法也又一點小小的問題,

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

    那么NullPointerException就會被raised.

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

    主站蜘蛛池模板: 日韩亚洲人成在线综合| 久久精品国产亚洲77777| 亚洲人成色99999在线观看| 久久不见久久见免费视频7| 亚洲av午夜成人片精品网站| 两性色午夜视频免费播放| 在线亚洲人成电影网站色www| 特级aa**毛片免费观看| 四虎亚洲国产成人久久精品| 深夜福利在线免费观看| 亚洲精品国精品久久99热| 一个人免费观看www视频| 国产亚洲?V无码?V男人的天堂 | 亚洲综合精品成人| 男女免费观看在线爽爽爽视频| 亚洲理论片在线中文字幕| 国内精品乱码卡1卡2卡3免费| 国产精品亚洲精品| 国产成人精品免费直播| 九一在线完整视频免费观看| 亚洲自偷自偷图片| 91大神在线免费观看| 四虎亚洲精品高清在线观看| 午夜寂寞在线一级观看免费| 特级aa**毛片免费观看| 国产A在亚洲线播放| 免费视频爱爱太爽了| 久久亚洲中文无码咪咪爱| 亚洲日韩国产成网在线观看| 日韩a级无码免费视频| 亚洲美女激情视频| 好大好深好猛好爽视频免费| 免费无遮挡无遮羞在线看| 亚洲成a人片在线观看日本| 99国产精品永久免费视频| 黄色a三级免费看| 亚洲另类激情综合偷自拍| 成人毛片视频免费网站观看| 九九99热免费最新版| 亚洲av无码一区二区三区天堂古代 | 久久久久亚洲AV综合波多野结衣|