<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在线丝| 亚洲国产一级在线观看| 亚洲男人的天堂网站| 免费在线观看的黄色网址| 国产无遮挡吃胸膜奶免费看| 免费国产高清毛不卡片基地| 午夜小视频免费观看| 免费国产草莓视频在线观看黄| 亚洲精品国精品久久99热一| 本免费AV无码专区一区| 亚洲一区二区三区高清| 麻豆成人久久精品二区三区免费| 亚洲欧美日韩中文无线码 | 精品久久久久久亚洲精品| 热99re久久免费视精品频软件| 免费无码一区二区三区蜜桃| 青青草原亚洲视频| 免费无码肉片在线观看| 精品久久久久久亚洲精品| 在线亚洲人成电影网站色www| 最近免费中文字幕视频高清在线看 | 一区二区三区精品高清视频免费在线播放| 亚洲女同成av人片在线观看| 免费无遮挡无码视频网站| 国产精品免费AV片在线观看| 国内成人精品亚洲日本语音| 亚洲精品视频观看| 最新中文字幕电影免费观看| 大地资源网高清在线观看免费| 在线91精品亚洲网站精品成人| 亚洲不卡1卡2卡三卡2021麻豆| 亚洲中文字幕无码一区二区三区| 白白国产永久免费视频| 久久久久久精品免费免费自慰| 国产成人亚洲精品| 国产男女猛烈无遮挡免费视频网站 | 狠狠色伊人亚洲综合网站色 | 亚洲经典千人经典日产| 亚洲va精品中文字幕| 亚洲综合成人网在线观看| 亚洲人成图片小说网站|