我在src下面放了一个properties文件 用测试类的main方法可以得到 启动Tomcat后在拦截器里面得不到
InputStream inStream = PropertiesUtil.class.getClassLoader().getSystemResourceAsStream(file); 
Debug后 发现inStream = null why? 难道上述语句不是加载WEB-INF下的东东?
有没有在不写路径情况下读取到WEB-INF/classes中properties文件的方法public static String getPropertByPath(String filePath ,String fileName ,String propKey) throws IOException{
   //构造文件路径
   String  file = fileName ;
   //如果路径为空则默认是src下的文件
   if(filePath!=null&&!"".equals(filePath.trim())) file = filePath+"/"+fileName;
   //读取文件并构造成文件流
   InputStream inStream = PropertiesUtil.class.getClassLoader().getSystemResourceAsStream(file);
   //构造属性并装载文件
   Properties properties = new Properties();
   properties.load(inStream);
   return (String)properties.get(propKey);
}

解决方案 »

  1.   

    ps = new Properties();
    fis = new FileInputStream(getPath() + "config.txt");
    ps.load(new DataInputStream(fis));
    ps.getPropert("dasfdsaf");
      

  2.   

    在类的main方法中读取src可以不写
      

  3.   

    那是因为main方法文件和资源文件的相对路径是一样的
      

  4.   

    InputStream inStream = PropertiesUtil.class.getClassLoader().getSystemResourceAsStream(file); 
    这个语句加载的就是web-inf/classes下面的东西了
      

  5.   

    java.util.ResourceBundle 中的getResourceAsStream
    参考http://blog.csdn.net/frank3g/article/details/3340858
      

  6.   

    1、可以将properties文件的路径配置到web.xml的参数中,项目中可以直接读取。
    2、过滤器的初始化时,FilterConfig类可以通过getServletContext().getRealPath("WEB-INF/classes/")来取到class路径。