- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we read & write a file using Gson streaming API in Java?
We can read and write a file using Gson streaming API and it is based on sequential read and write standard. The JsonWriter and JsonReader are the core classes built for streaming write and read in Streaming API. The JsonWriter writes a JSON encoded value to a stream, one token at a time. The stream includes both literal values (strings, numbers, booleans, and nulls) as well as begin and end delimiters of objects and arrays and JsonReader reads a JSON encoded value as a stream of tokens. This stream includes both literal values (strings, numbers, booleans, and nulls) as well as the begin and end delimiters of objects and arrays. The tokens are traversed in depth-first order, the same order that they appear in the JSON document.
Write to a file using JsonWriter
Example
import java.io.*; import com.google.gson.stream.*; public class JsonWriterTest { public static void main(String args[]) { JsonWriter writer; try { writer = new JsonWriter(new FileWriter("input.json")); writer.beginObject(); writer.name("name").value("Adithya"); writer.name("age").value(25); writer.name("technologies"); writer.beginArray(); writer.value("Java"); writer.value("Scala"); writer.value("Python"); writer.endArray(); writer.endObject(); writer.close(); System.out.println("Data write to a file successfully"); } catch(Exception e) { e.printStackTrace(); } } }
Output
Data write to a file successfully
Read a file using JsonReader
Example
import java.io.*; import com.google.gson.stream.*; public class JsonReaderTest { public static void main(String args[]) { JsonReader reader; try { reader = new JsonReader(new FileReader("input.json")); reader.beginObject(); while(reader.hasNext()) { String name = reader.nextName(); if(name.equals("name")) { System.out.println(reader.nextString()); } else if(name.equals("age")) { System.out.println(reader.nextInt()); } else if(name.equals("technologies")) { reader.beginArray(); while(reader.hasNext()) { System.out.println(reader.nextString()); } reader.endArray(); } else { reader.skipValue(); } } reader.endObject(); reader.close(); } catch(Exception e) { e.printStackTrace(); } } }
Output
Adithya 25 Java Scala Python
- Related Articles
- How can we implement a JSON array using Streaming API in Java?
- How can we read a JSON file in Java?
- How to parse a JSON string using Streaming API in Java?
- How can we use @Since annotation using Gson in Java?
- How to write a JSON string to file using the Gson library in Java?
- How can we write JSON objects to a file in Java?
- How to read and write a file using Javascript?
- Check whether we can write to a file in Java
- How can we import a gson library in JShell in Java 9?
- Can we use readUTF() to read a string from a .txt file in Java?
- How can we implement Flow API using Publisher-Subscriber in Java 9?
- Read/Write structure to a file using C
- How can we update an existing JSON data using javax.json API in Java?
- How to read/write data from/to .properties file in Java?
- How to read integers from a file using BufferedReader in Java?

Advertisements