write Object to file - Android File Input Output

Android examples for File Input Output:Object Serialization

Description

write Object to file

Demo Code


//package com.java2s;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

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

public class Main {
    public static void writeObj(Object obj, File dir, String fileName) {

        try {/*  ww w .  j  a va  2s.  co  m*/

            writeObj(obj, new File(dir, fileName));

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    public static void writeObj(Object obj, File f) {
        ObjectOutputStream oos = null;
        try {

            oos = new ObjectOutputStream(new FileOutputStream(f));
            oos.writeObject(obj);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeOutStream(oos);
        }
    }

    public static void closeOutStream(OutputStream output) {
        try {
            if (output != null)
                output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials