温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/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 雪山飞鹄 阅读(5374) 评论(1)  编辑  收藏 所属分类: javase

Feedback

# re: Java加载src目录下文件的几种方式,谈谈你的见解 2010-08-02 13:21 Mercy
博主的方法也又一点小小的问题,

如果执行并且没有限制RuntimePermission("setContextClassLoader")的话:
Thread.currentThread().setContextClassLoader(null);

那么NullPointerException就会被raised.

所以,还要利用
ClassLoader.getSystemClassLoader()来弥补这个问题。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: