I have to implement a temporary "persistence" solution for retrieving some definitions ( simple json strings). I have a rest endpoint which creates an instance of my object and then I want to write the json definition inside a file.
I currently have a class loader which loads files from the resources folder inside my module, I use these to grab the query results. Now I want to save a new definition, and write it to a file inside the resources folder.
However, I am unsuccessful in doing so. I grab the path from the existing file with the classloader and when I try to create a new file using FileUtils, it either throws some errors, or creates the file in D:.
Is there a way to use the resources folder to add/edit files at runtime ? Here is some code also with one of the many ways i've tried it.
public String writeDocument(String path, String content) throws IOException {
\\Example call: writeDocument("/definitions/somedefinition.txt",jsonStringHere);
\\where /definitions/somedefinition.txt is placed in /resources
ClassLoader classLoader = getClass().getClassLoader();
final URL url = classLoader.getResource(path);
final File file;
String finalPath = null;
try {
file = Paths.get(url.toURI()).toFile();
FileUtils.writeStringToFile(file, content);
finalPath = file.getAbsolutePath();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return finalPath;
}