Accessing files in specific folder in classpath using Java

后端 未结 3 1841
我寻月下人不归
我寻月下人不归 2020-12-16 18:04

I want to read a bunch of text files in com.example.resources package. I can read a single file using the following code:

InputStream is = MyObject.class.get         


        
3条回答
  •  时光说笑
    2020-12-16 18:43

    If you pass in a directory to the getResourceAsStream method then it will return a listing of files in the directory ( or at least a stream of it).

    Thread.currentThread().getContextClassLoader().getResourceAsStream(...)
    

    I purposely used the Thread to get the resource because it will ensure I get the parent class loader. This is important in a Java EE environment however probably not too much for your case.

提交回复
热议问题