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

前端 未结 5 1729
野的像风
野的像风 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:26

    Don't try to access a resource like a file. Just grab the InputStream and read the data from there:

    byte[] data;
    try (InputStream in = getClass().getResourceAsStream("/elasticsearch/segmentsIndex.json")) {
        data = in.readAllBytes​(); // usable in Java 9+
        // data = IOUtils.toByteArray(in); // uses Apache commons IO library
    }
    

    This example uses the IOUtils class from Apache commons-io library.

    If you are targeting Java 9+ you can alternatively use data = in.readAllBytes​();.

提交回复
热议问题