java.nio.file.FileSystemNotFoundException when getting file from resources folder

前端 未结 5 1728
野的像风
野的像风 2021-01-04 11:58

I am getting this error on the following code (note that this does not happen on my local machine, only on my build server):

Files.readAllBytes(Paths.get(get         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-04 12:17

    You should get the resource through an InputStream and not a File, but there is no need for external libraries.

    All you need is a couple of lines of code:

    InputStream is = getClass().getResourceAsStream("/elasticsearch/segmentsIndex.json");
    java.util.Scanner scanner = new java.util.Scanner(is).useDelimiter("\\A");
    String json = scanner.hasNext() ? scanner.next() : "";
    

    You can learn more about that method at https://stackoverflow.com/a/5445161/968244

提交回复
热议问题