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

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

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

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

    雪山飛鵠

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

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      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()方法時括號中資源文件名稱前的/不能省略
            

    Feedback

    # re: Java加載src目錄下文件的幾種方式,談?wù)勀愕囊娊?a name="Post"> 2010-08-02 13:21 Mercy
    博主的方法也又一點小小的問題,

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

    那么NullPointerException就會被raised.

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

    主站蜘蛛池模板: 国产精品视频免费观看| 伊人免费在线观看高清版| 最近中文字幕大全免费视频| 亚洲永久精品ww47| 免费在线观看一区| 久久精品国产亚洲5555| 亚洲精品国产日韩无码AV永久免费网| 日本19禁啪啪无遮挡免费动图| 亚洲欧美日韩中文二区| 好爽…又高潮了免费毛片| 亚洲熟妇AV日韩熟妇在线| 啦啦啦中文在线观看电视剧免费版 | 亚洲精品在线免费观看| 在线成人爽a毛片免费软件| 中文字幕亚洲综合久久| 2021免费日韩视频网| 伊人久久亚洲综合影院首页| 女人张腿给男人桶视频免费版| 综合一区自拍亚洲综合图区| 国产一级淫片免费播放电影| 色婷婷综合缴情综免费观看| 亚洲人成网站在线播放vr| 日韩视频在线观看免费| 亚洲国产成人久久| 午夜一区二区免费视频| 又黄又大的激情视频在线观看免费视频社区在线 | 狠狠综合久久综合88亚洲| 日本免费高清视频| 亚洲精品亚洲人成在线播放| 日本不卡视频免费| 91av免费在线视频| 亚洲综合精品一二三区在线| 最近的免费中文字幕视频 | 久久青草精品38国产免费| 亚洲性猛交xx乱| 免费a级毛片大学生免费观看 | 国产精品亚洲二区在线观看 | 日韩在线观看免费完整版视频| 久久精品国产亚洲网站| 日韩中文字幕精品免费一区| 国产成人精品亚洲一区|