save Object To File - Android File Input Output

Android examples for File Input Output:Object Serialization

Description

save Object To File

Demo Code


//package com.java2s;

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

import java.io.ObjectOutputStream;
import android.app.Activity;
import android.content.Context;
import android.util.Log;

public class Main {
    public static void saveObjectToFile(Activity owner, String filename,
            Object objectToSave) {
        try {// w w w  . ja  v  a 2 s . co m
            FileOutputStream fos = owner.openFileOutput(filename,
                    Context.MODE_PRIVATE);
            ObjectOutputStream objOut = new ObjectOutputStream(fos);
            objOut.writeObject(objectToSave);
            objOut.close();
            fos.close();
        } catch (IOException e) {
            Log.i(owner.getClass().getSimpleName(),
                    "Error writing output file " + filename + ".");
        }
    }
}

Related Tutorials