
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to write a JSON string to file using the Gson library in Java?
A Gson is a library that can be used to convert Java Objects to JSON representation. The primary class to use is Gson which we can create by calling the new Gson() and the GsonBuilder class can be used to create a Gson instance.
We can write a JSON string to file using the toJson() method of Gson class in the below example
Example
import java.io.*; import com.google.gson.*; public class JSONToFileTest { public static void main(String[] args) throws IOException { Gson gson = new Gson(); FileWriter fileWriter = new FileWriter("Student.json"); Student student = new Student("Raja", "Ramesh", 30, "Hyderabad"); gson.toJson(student, fileWriter); fileWriter.close(); System.out.println("JSON string write to a file successfully"); System.out.println(student); } } // Student class class Student { private String firstName; private String lastName; private int age; private String address; public Student(String firstName, String lastName, int age, String address) { super(); this.firstName = firstName; this.lastName = lastName; this.age = age; this.address = address; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "Student[ " + "firstName = " + firstName + ", lastName = " + lastName + ", age = " + age + ", address = " + address + " ]"; } }
Student.json file
Output
JSON string write to a file successfully Student[ firstName = Raja, lastName = Ramesh, age = 30, address = Hyderabad ]
- Related Articles
- How to convert Java object to JSON using GSON library?
- How to pretty print JSON using the Gson library in Java?
- Convert a Map to JSON using the Gson library in Java?
- Convert Java object to JSON using the Gson library in Java?\n
- Convert a list of objects to JSON using the Gson library in Java?
- Convert JSON object to Java object using Gson library in Java?\n
- How to Write/create a JSON file using Java?
- How to format a date using the Gson library in Java?
- Convert a JSON String to Java Object using the json-simple library in Java?\n
- How to add/insert additional property to JSON string using Gson in Java?\n
- How to serialize a null field using Gson library in Java?
- How to use @Until annotation using the Gson library in Java?
- How to rename the properties of JSON using Gson in Java?
- How to parse a JSON without duplicate keys using Gson in Java?
- How to add a JSON string to an existing JSON file in Java?

Advertisements