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

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

    Generally, it is not correct to assume that every resource is a file. Instead, you should obtain the URL/InputStream for that resource and read the bytes from there. Guava can help:

    URL url = getClass().getResource("/elasticsearch/segmentsIndex.json");
    String content = Resources.toString(url, charset);
    


    Another possible solution, with the InputStream and apache commons: Convert InputStream to byte array in Java .

    From a byte[], simply use the String constructor to obtain the content as a string.

提交回复
热议问题