Reading resource file inside jar dependency

前端 未结 2 1249
北恋
北恋 2021-01-21 22:16

Before I explain my problem - I\'ve tried to find solution in already asked questions, but none of them worked ;)

I\'m trying to read file inside jar, which is a depende

相关标签:
2条回答
  • 2021-01-21 23:16

    If you want to read a file from inside a jar file, maybe the next line works better:

    InputStream in = this.getClass().getResourceAsStream("/driver/filename.txt");
    

    After this you will need to convert the InputStream in to a String. I've used Apache commons IOUtils:

    String theString = IOUtils.toString(inputStream, encoding);
    

    I hope it helps...

    0 讨论(0)
  • 2021-01-21 23:22

    Use target jar classloader like this

    ClassInOtherJar.class.getClassLoader().getResourceAsStream("filename.xml") 
    
    0 讨论(0)
提交回复
热议问题