Different ways of loading a file as an InputStream

前端 未结 6 667
予麋鹿
予麋鹿 2020-11-22 04:02

What\'s the difference between:

InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName)

and

InputSt         


        
6条回答
  •  执念已碎
    2020-11-22 04:22

    All these answers around here, as well as the answers in this question, suggest that loading absolute URLs, like "/foo/bar.properties" treated the same by class.getResourceAsStream(String) and class.getClassLoader().getResourceAsStream(String). This is NOT the case, at least not in my Tomcat configuration/version (currently 7.0.40).

    MyClass.class.getResourceAsStream("/foo/bar.properties"); // works!  
    MyClass.class.getClassLoader().getResourceAsStream("/foo/bar.properties"); // does NOT work!
    

    Sorry, I have absolutely no satisfying explanation, but I guess that tomcat does dirty tricks and his black magic with the classloaders and cause the difference. I always used class.getResourceAsStream(String) in the past and haven't had any problems.

    PS: I also posted this over here

提交回复
热议问题