Loading external file as resource

Nikhil Gupta :

I am loading resource files like following

public static JSONArray readConfig(String fileName) throws IOException, JSONException{
    ClassPathResource res = new ClassPathResource(fileName);    
    byte[] bdata = FileCopyUtils.copyToByteArray(res.getInputStream());
    String json = new String(bdata, StandardCharsets.UTF_8);
    JSONArray jarr = new JSONArray(json);
    return jarr;
};

I would like to know if its possible to add an external location/file like "/something/something1/config.json" to the resources/classpath so that it can be read by the same code as above.

Thanks...

Ruelos Joel :

You can add the folder location in the classpath.

java -Dloader.path="/something/something1/" -jar your-app.jar

see link for loader.path documentation

If you want to load a resource that is not in the classpath, use FileSystemResource instead of ClassPathResource so it will load the resource in the file system.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357776&siteId=1