Write JSON data to a file in Java
This post will discuss how to write JSON data to a file in Java.
1. Using PrintWriter
A simple solution is to create a new PrintWriter instance from the character-output stream and use its write() method to write the JSON string to a file. You can construct and pass a FileWriter given a file name, using the specified charset or the platform’s default one. Note that if the file does not exist, a new file will be created; otherwise, the existing file will be truncated.
The following solution uses JSON-Java library to create companies.json file, at the specified location, with the JSON content {"offices":["Mountain View","Los Angeles","New York"],"name":"Google","employees":140000}.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import org.json.JSONException; import org.json.JSONObject; import java.io.FileWriter; import java.io.PrintWriter; import java.util.List; public class Main { public static void main(String[] args) { String path = "/app/json/companies.json"; JSONObject json = new JSONObject(); try { json.put("name", "Google"); json.put("employees", 140000); json.put("offices", List.of("Mountain View", "Los Angeles", "New York")); } catch (JSONException e) { e.printStackTrace(); } try (PrintWriter out = new PrintWriter(new FileWriter(path))) { out.write(json.toString()); } catch (Exception e) { e.printStackTrace(); } } } |
Here’s an alternative solution that uses the Gson Java library to convert an object to its JSON representation. Gson provides the toJson() method to convert a Java object to a JSON string and write it to a file using PrintWriter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import com.google.gson.Gson; import java.io.FileWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.List; class Company { public String name; public int employees; public List<String> offices; public Company(String name, int employees, List<String> offices) { this.name = name; this.employees = employees; this.offices = offices; } } public class Main { public static void main(String[] args) { Company companies = new Company("Google", 140000, Arrays.asList("Mountain View", "Los Angeles", "New York")); String path = "/app/json/companies.json"; try (PrintWriter out = new PrintWriter(new FileWriter(path))) { Gson gson = new Gson(); String jsonString = gson.toJson(companies); out.write(jsonString); } catch (Exception e) { e.printStackTrace(); } } } |
2. Using Jackson library
Jackson is a high-performance library for processing JSON data in Java. It provides an ObjectMapper class for reading and writing JSON. The idea is to use the writeValue() method to serialize Java objects as JSON String and write the JSON to the supplied file. The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import org.codehaus.jackson.map.ObjectMapper; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.List; class Company { public String name; public int employees; public List<String> offices; public Company(String name, int employees, List<String> offices) { this.name = name; this.employees = employees; this.offices = offices; } } public class Main { public static void main(String[] args) { Company companies = new Company("Google", 140000, Arrays.asList("Mountain View", "Los Angeles", "New York")); String path = "/app/json/companies.json"; try { ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(new File(path), companies); } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about writing JSON data to a file in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)