I have a maven project and need to write JSON objects in file.json that should be located in
src/main/resources/
But before I need to check if file exists. If not than create it.
public static void writeMenuJSon(String jsonParamIn) {
JsonParser parser = new JsonParser();
JsonObject modulJson = parser.parse(jsonParamIn).getAsJsonObject();
//TODO check if file exists and create file if not
code here ...
// write to file JSON object
try (Writer writer = new FileWriter("path to file")){
Gson gson = new GsonBuilder().create();
gson.toJson(modulJson, writer);
} catch (IOException e) {
e.printStackTrace();
}
}