Android Object Serialization writeObjectToFile(Serializable obj, String filename)

Here you can find the source of writeObjectToFile(Serializable obj, String filename)

Description

Write a serializable object to a file

Parameter

Parameter Description
obj the object to write
filename the file to write it to

Return

true iff successful

Declaration

public static boolean writeObjectToFile(Serializable obj,
        String filename) 

Method Source Code

//package com.java2s;

import java.io.BufferedOutputStream;

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

import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public class Main {
    /**//  www  . j  av a 2 s . co  m
     * Write a serializable object to a file
     * @param obj the object to write
     * @param filename the file to write it to
     * @return true iff successful
     */
    public static boolean writeObjectToFile(Serializable obj,
            String filename) {
        //serialize the object

        try {
            //use buffering
            OutputStream file = new FileOutputStream(filename);
            OutputStream buffer = new BufferedOutputStream(file);
            ObjectOutput output = new ObjectOutputStream(buffer);
            try {
                output.writeObject(obj);
            } finally {
                output.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }

        return true;
    }
}

Related

  1. loadObjectFromFile(String filename)
  2. readObjectFromFile(File fileLocation)
  3. readObjectFromFile(String filename)
  4. saveObjectAsFile(String filename, Object object)
  5. writeObjectToFile(File fileLocation, Object obj)
  6. objectToString(Object o)
  7. stringToObject(String s)
  8. getBytes(Serializable obj)
  9. deserialize(final File file, final long serialTtl)