read Object from File cache - Android File Input Output

Android examples for File Input Output:Object Serialization

Description

read Object from File cache

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.ObjectInputStream;

import java.io.StreamCorruptedException;
import android.text.TextUtils;

public class Main {

    public static Object readObject(String filePath) {
        Object _obj = null;/*from  w  w  w.j a  v a 2s.c o m*/

        if (!TextUtils.isEmpty(filePath)) {
            File _cachedFile = new File(filePath);

            ObjectInputStream _objInStream = null;
            try {
                _objInStream = new ObjectInputStream(new FileInputStream(
                        _cachedFile));
                //??????
                _obj = _objInStream.readObject();
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (null != _objInStream) {
                    try {
                        _objInStream.close();
                        _objInStream = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return _obj;
    }
}

Related Tutorials