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

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

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

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

    雪山飛鵠

    溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://m.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()來彌補這個問題。  回復  更多評論
      

    主站蜘蛛池模板: 国产精品亚洲二区在线| 亚洲国产高清视频在线观看| 亚洲精品国产综合久久久久紧| 99久久久国产精品免费牛牛四川| 亚洲AV无码成人网站久久精品大 | 免费无码中文字幕A级毛片| 国产亚洲精品一品区99热| 青青操免费在线视频| 久久精品国产精品亚洲艾草网| 中文字幕手机在线免费看电影 | 亚洲Av无码乱码在线znlu| 大桥未久亚洲无av码在线| 免费A级毛片无码A| 一区二区在线视频免费观看| 国产a v无码专区亚洲av| a毛片免费观看完整| 亚洲成人动漫在线| 无码精品A∨在线观看免费| 亚洲性色精品一区二区在线| 日本一道综合久久aⅴ免费| 一级特黄a免费大片| 亚洲AV日韩精品久久久久久久| 免费观看激色视频网站(性色)| 亚洲一区精品视频在线| 国产一级大片免费看| 国产一级一毛免费黄片| 亚洲免费在线视频| 女人18毛片a级毛片免费| 一级毛片aa高清免费观看| 亚洲国产精品热久久| 手机在线看永久av片免费| 美女免费视频一区二区三区| 亚洲国产精品VA在线观看麻豆| 57pao一国产成永久免费| 亚洲精品av无码喷奶水糖心| 亚洲一区二区三区在线播放| 无码日韩精品一区二区三区免费| 国产亚洲国产bv网站在线| 亚洲综合最新无码专区| 五月婷婷在线免费观看| 成人免费网站视频www|