Different ways of loading a file as an InputStream

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

What\'s the difference between:

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

and

InputSt         


        
6条回答
  •  逝去的感伤
    2020-11-22 04:29

    Use MyClass.class.getClassLoader().getResourceAsStream(path) to load resource associated with your code. Use MyClass.class.getResourceAsStream(path) as a shortcut, and for resources packaged within your class' package.

    Use Thread.currentThread().getContextClassLoader().getResourceAsStream(path) to get resources that are part of client code, not tightly bounds to the calling code. You should be careful with this as the thread context class loader could be pointing at anything.

提交回复
热议问题