Java ObjectOutputStream Write saveObject2File(final Serializable object, final File file)

Here you can find the source of saveObject2File(final Serializable object, final File file)

Description

save Object File

License

Apache License

Declaration

public static void saveObject2File(final Serializable object, final File file) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

import java.util.zip.DeflaterOutputStream;

public class Main {
    public static void saveObject2File(final Serializable object, final File file) {
        try {//from   w w w.j  a v  a 2  s. c o m
            saveObject2FileImpl(object, file);
        } catch (final Exception _exception) {
            throw new RuntimeException(
                    "Problems saving object [" + object + "] to file [" + file + "]: " + _exception.getMessage(),
                    _exception);
        }
    }

    private static void saveObject2FileImpl(final Serializable object, final File file) throws IOException {
        ObjectOutputStream output = null;

        try {
            output = new ObjectOutputStream(
                    new BufferedOutputStream(new DeflaterOutputStream(new FileOutputStream(file)), 1024 * 1024));

            output.writeObject(object);

            output.flush();
            output.close();
            output = null;
        } finally {
            if (output != null) {
                output.close();
            }
        }
    }
}

Related

  1. saveObject(Serializable obj, String url)
  2. saveObject(String filename, Object obj)
  3. saveObject(String filename, Object obj)
  4. saveObject(String filePath, Serializable obj)
  5. saveObject(T object, File file, boolean use_compression)
  6. saveObject2FileImpl(final Serializable object, final File file)
  7. saveObjectInFile(String fileName, Object obj)
  8. saveObjectToFile(File file, Object object)
  9. saveObjectToFile(File thatFile, Object... thisObject)