read Cache as Serializable - Android App

Android examples for App:Cache

Description

read Cache as Serializable

Demo Code


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import android.content.Context;

public class Main{
    public static Serializable readCache(Context context, String key) {
        String encrypt_key = CipherUtil.encrypt2md5(key);
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {//www .j av  a 2s .  c  om
            fis = context.openFileInput(encrypt_key);
            ois = new ObjectInputStream(fis);
            return (Serializable) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                ois.close();
            } catch (Exception e) {
            }
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
        return null;
    }
}

Related Tutorials