Reading resource file inside jar dependency

前端 未结 2 1254
北恋
北恋 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...

提交回复
热议问题