getClass().getResourceAsStream("/a.properties"), 如果不带/的话将返回null.
看了jdk的文档:
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."after making these changes......."没有说一定要带/.我的理解是, 默认的class loader会从classpath所在的目录去找, 所以需要带个/另外, 我在unix上面使用的时候, 不需要带/我在windows上用ClassLoader.getSystemResourceAsStream的时候, 不需要带/
如果文件有自己的包名, 必须带/, 比如ClassLoader.getSystemResourceAsStream("/com.xx.yy.a.properties")总之带/肯定没错, 谁有更好的解释?

解决方案 »

  1.   

    在使用Class.getResourceAsStream 时, 资源路径有两种方式, 一种以 / 开头,则这样的路径是指定绝对
    路径, 如果不以 / 开头, 则路径是相对与这个class所在的包的。
    如:把commom_url.properties 放到 com.lcb.path.test下
    ClassName.class.getResourceAsStream("commom_url.properties") 或ClassName.class.getResourceAsStream("/com/lcb/path/test/"+"commom_url.properties")
    把commom_url.properties文件放在Classes文件夹中的时ClassName.class.getResourceAsStream("/commom_url.properties")