Create new file and add whole json object to file - Java JSON

Java examples for JSON:JSON Data

Description

Create new file and add whole json object to file

Demo Code


//package com.java2s;
import javax.json.Json;
import javax.json.JsonObject;

import javax.json.JsonWriter;
import java.io.File;

import java.io.FileWriter;

public class Main {
    /**//from ww w  .  j av  a  2  s  .  c  om
     * Create new file and add whole json object to file
     *
     * @param url
     * @param jsonObject
     * @throws Exception
     */
    public static void writeJSON(String url, JsonObject jsonObject)
            throws Exception {
        try {
            String path = new File("").getAbsolutePath() + url;
            JsonWriter writer = Json.createWriter(new FileWriter(path));

            writer.writeObject(jsonObject);
            writer.close();
        } catch (Exception e) {
            throw e;
        }
    }
}

Related Tutorials