public static Properties getProperties(String name){
InputStream in = ClassLoader.getSystemResourceAsStream("config/"+name+".properties");
Properties properties = new Properties();
try {
properties.load(in);
in.close();
in = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
properties = null;
}
return properties;
}这个方法是我写的一个读取配置文件的方法,在MAIN方法里面测试通过,但是在serlvet里面调用,这个方法总是执行不了,我想是不是资源不够,用个线程来执行还是不行,请大侠们帮忙指点下,小弟不甚感激~~~

解决方案 »

  1.   

    debug一下,很容易定位估计是路径的问题,要搞清楚绝对路径和相对路径。
      

  2.   

      InputStream in = ClassLoader.getSystemResourceAsStream("config/"+name+".properties");
    路径明显是相对路径....
      

  3.   

    晕,不是路径问题,我在main方法里面测试过的,我的意思是这个方法执行到流的时候就好像停止了,也没有报错~~~
      

  4.   

    properties文件有好几种读取方式呢,你写的或许不适用于当前场景,才导致异常建议到网上查找下,换别的读取方式试试,要不试下下边的示例:
    Properties prop = new Properties();
    InputStream in = new FileInputStream(configFilePath);
    prop.load(in);
      

  5.   

    大家看看代码和执行结果吧!public static Properties getProperties(String name){
    InputStream in = ClassLoader.getSystemResourceAsStream("config/"+name+".properties");
    System.out.println(ClassLoader.getSystemResourceAsStream("config/"+name+".properties"));
    Properties properties = new Properties();
    try {
    properties.load(in);
    in.close();
    in = null;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    properties = null;
    }
    return properties;
    }

    public static void main(String[] args) {
    System.out.println("out:"+getProperties("config").getProperty("cacheSize"));
    }
    结果:out:104857600
      

  6.   

    在servlet里读写文件,获取路径应该是:request.getRealPath("相对路径")
      

  7.   

    已经定性,路径问题。
    servlet中访问应用资源的路径使用绝对路径最好。
      

  8.   

    java工程和web工程的路径不一样的
      

  9.   

    在main方法里面测试过,但在Servlet里面是不一样的,你那个配置文件换个位置,或者改改路径