Java ObjectOutputStream Write saveObjectToFile(final Object object, final String fileName)

Here you can find the source of saveObjectToFile(final Object object, final String fileName)

Description

save Object To File

License

Open Source License

Declaration

public static void saveObjectToFile(final Object object, final String fileName) 

Method Source Code

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

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

import java.io.ObjectOutputStream;

public class Main {
    public static void saveObjectToFile(final Object object, final String fileName) {
        try {/*from   w w  w  .  j  av  a  2 s  .co m*/
            // TODO: this code could be optimized in a way that a new method is provide for sets of
            // strings where only the new strings are appended to the file
            FileOutputStream fileOutputStream = new FileOutputStream(fileName);
            ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);
            oos.writeObject(object);
            oos.flush();
            oos.close();
        } catch (IOException e) {
            throw new RuntimeException("Could not save '" + object + "' to file '" + fileName + "':  " + e);
        }
    }
}

Related

  1. saveObject2File(final Serializable object, final File file)
  2. saveObject2FileImpl(final Serializable object, final File file)
  3. saveObjectInFile(String fileName, Object obj)
  4. saveObjectToFile(File file, Object object)
  5. saveObjectToFile(File thatFile, Object... thisObject)
  6. saveObjectToFile(Object obj, String fileName)
  7. saveObjectToPath(T objectToSave, String pathToSaveTo)
  8. saveParameters()
  9. saveResultToStream(ObjectOutputStream oos, Object result)