Java ObjectOutputStream Write saveToFile(Serializable object, File file, boolean compress)

Here you can find the source of saveToFile(Serializable object, File file, boolean compress)

Description

save To File

License

GNU General Public License

Declaration

public static void saveToFile(Serializable object, File file, boolean compress) 

Method Source Code

//package com.java2s;
// This file is part of MarMoT, which is licensed under GPLv3.

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.ObjectOutputStream;
import java.io.OutputStream;

import java.io.Serializable;

import java.util.zip.GZIPOutputStream;

public class Main {
    public static void saveToFile(Serializable object, String filepath) {
        saveToFile(object, new File(filepath), true);
    }//from w w w.  j a v a2s. c  om

    public static void saveToFile(Serializable object, File file, boolean compress) {
        try {

            OutputStream ostream = new FileOutputStream(file);
            if (compress) {
                ostream = new GZIPOutputStream(ostream);
            }

            ObjectOutputStream stream = new ObjectOutputStream(ostream);
            stream.writeObject(object);
            stream.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void saveToFile(Serializable object, File file) {
        saveToFile(object, file, true);
    }
}

Related

  1. saveSerializableObject(Object obj, String path)
  2. saveSerialized(Serializable obj, File file)
  3. saveStringToFile(String filepath, String content)
  4. saveToFile(File f, Object obj)
  5. saveToFile(final File file, final Object obj)
  6. saveToFile(String string, File file, boolean append)