Here you can find the source of writeObjectToFile(Object object, File file)
public static void writeObjectToFile(Object object, File file) throws Exception
//package com.java2s; //License from project: Open Source License import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.io.File; import java.io.FileWriter; public class Main { private final static Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create(); public static void writeObjectToFile(Object object, File file) throws Exception { if (!file.exists()) { file.createNewFile();//from w w w . j ava 2s . c o m } FileWriter writer = new FileWriter(file); writer.write(gson.toJson(object)); writer.close(); } }