write json string to file in java 1

write json string to file in java

// Write JSON string to a file
try (FileWriter file = new FileWriter("fileName.json")) {
  file.write(data); // data is a JSON string here
  file.flush();
} catch (IOException e) {
  e.printStackTrace();
}

Here is what the above code is Doing:
1. Create a JSONObject and add some data to it.
2. Create a FileWriter and write the JSONObject to it.
3. Flush the FileWriter.

Similar Posts