方法getResourceAsStream()是一个标准方法,用来打开一个外部输入文件。文件的位置取决于类加载器,而标准的类加载器从classpath开始搜索。

解决方案 »

  1.   

    public InputStream getResourceAsStream(String name)Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. 
    This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/". If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.getSystemResourceAsStream.
      

  2.   

    如:c:\temp\mytest\Test.classpackage mytest;
    public class Test{...}Test.class.getResourceAsStream(str);str="/db.properties" 则 db.properties 放在 c:\temp下
    str="db.properties" 则 db.properties 放在c:\temp\mytest下