简体   繁体   中英

Difference between methods to getResourceAsStream

有人可以告诉我MyClass.class.getClassLoader().getResourceAsStream("TestFile.txt")MyClass.class.getResourceAsStream("TestFile.txt")什么区别

They are (more or less) equivalent. Class#getResourceAsStream() will get the ClassLoader internally and delegate the getResourceAsStream call to it.

Thanks to VGR's comments . It's important to note that Class#getResourceAsStream(String) first delegates to Class#getResource(String) which, if it doesn't start with a / , transforms the String argument passed by pre-pending the package name of the corresponding class with a / replacing each . .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM