<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()來彌補這個問題。  回復  更多評論
      

    主站蜘蛛池模板: 亚洲日韩精品无码专区加勒比| 午夜亚洲www湿好大| 精品久久亚洲中文无码| 182tv免费观看在线视频| 久久精品夜色国产亚洲av| 永久在线观看免费视频| 好看的电影网站亚洲一区| 免费无码一区二区三区蜜桃| 亚洲熟妇无码另类久久久| 亚洲一区二区三区免费| 中文字幕精品亚洲无线码一区| 丁香六月婷婷精品免费观看| 亚洲综合色在线观看亚洲| 99麻豆久久久国产精品免费| 国产精品久久久亚洲| 久草免费手机视频| 亚洲第一永久在线观看| 妻子5免费完整高清电视| 亚洲av永久无码精品秋霞电影秋| 又粗又硬又大又爽免费视频播放| 男女交性无遮挡免费视频| 亚洲国产精品嫩草影院在线观看| 日本视频在线观看永久免费| 亚洲国产精品yw在线观看| 国产免费不卡v片在线观看| 亚洲丁香婷婷综合久久| 亚洲精品无码AV中文字幕电影网站| 久久久久国色AV免费观看| 久久精品a亚洲国产v高清不卡| 成人黄色免费网址| 高h视频在线免费观看| 亚洲成AV人在线播放无码| 国产在线a免费观看| 特黄特色的大片观看免费视频| 亚洲精品无码午夜福利中文字幕 | 99ri精品国产亚洲| 毛片免费视频在线观看| 免费无码午夜福利片 | 女人18毛片水最多免费观看| 国产精品亚洲av色欲三区| 亚洲春色在线视频|