Java Object Save writeObjectToFile(Object object, File file)

Here you can find the source of writeObjectToFile(Object object, File file)

Description

write Object To File

License

Open Source License

Declaration

public static void writeObjectToFile(Object object, File file) throws Exception 

Method Source Code


//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();
    }
}

Related

  1. writeObjectToFile(Object o, String filename)
  2. writeObjectToFile(Object o, String fileName)
  3. writeObjectToFile(Object obj, File f)
  4. writeObjectToFile(Object obj, String fileName)
  5. writeObjectToFile(Object obj, String path)
  6. writeObjectToFile(Object oObject, File destDir, String filename)
  7. writeObjectToFile(String dirname, String filename, Object obj)
  8. writeObjectToFile(String fileName, Object obj)
  9. writeObjectToFile(String filename, Serializable s)