Java Json String Create write2Json(String json, String path, String name)

Here you can find the source of write2Json(String json, String path, String name)

Description

write Json

License

Open Source License

Declaration

public static void write2Json(String json, String path, String name) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

    public static void write2Json(String json, String path, String name) throws IOException {
        File pathFile = new File(path);
        if (!pathFile.isDirectory()) {
            throw new FileNotFoundException();
        }//from   w  w  w  .  j a v  a2s.  c  o m
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }
        if (name == null) {
            name = getDefaultName();
        }
        File file = new File(path + "/" + name);

        FileWriter writer = new FileWriter(file, true);
        if (file.length() == 0) {
            writer.append("[");
        }
        writer.append(json + ",");
        writer.close();
    }

    private static String getDefaultName() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date()) + ".json";
    }
}

Related

  1. toJsonArrays(String... strings)
  2. toJsonField(String name, String value)
  3. toJsonName(String name, int prefixLen)
  4. toJsonUrl(String pUrl)
  5. toJSONValue(Enum e)