<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 雪山飛鵠 閱讀(5391) 評論(1)  編輯  收藏 所屬分類: javase

    Feedback

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

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

    那么NullPointerException就會被raised.

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

    主站蜘蛛池模板: 久久精品国产亚洲av品善| 亚洲免费电影网站| 一个人看的在线免费视频| 天堂在线免费观看中文版| 国内精品久久久久影院亚洲| 日本zzzzwww大片免费| 91午夜精品亚洲一区二区三区| 99精品视频在线观看免费播放| 亚洲AV无码精品色午夜果冻不卡 | 亚洲AV永久青草无码精品| 国产线视频精品免费观看视频| 国产亚洲美女精品久久久2020 | 精品久久久久久久免费人妻| 亚洲欧美日韩自偷自拍| 四虎成人精品在永久免费| 新最免费影视大全在线播放| 国产性爱在线观看亚洲黄色一级片| 中国一级全黄的免费观看| 亚洲精品中文字幕乱码影院| 97人伦色伦成人免费视频| 黄网站色视频免费看无下截| 亚洲五月午夜免费在线视频| 国产白丝无码免费视频| 亚洲图片校园春色| 老司机永久免费网站在线观看| 免费人成动漫在线播放r18| 亚洲欧洲日产国码无码久久99| 91视频免费网址| 亚洲综合无码一区二区痴汉| 精品国产亚洲男女在线线电影| 性xxxx视频免费播放直播| 77777亚洲午夜久久多喷| 日韩亚洲精品福利| 免费乱码中文字幕网站| 国产精品免费αv视频| 亚洲好看的理论片电影| 在线jyzzjyzz免费视频| 中文在线观看永久免费| 亚洲综合色一区二区三区| 亚洲日本乱码在线观看| 成年午夜视频免费观看视频|