码迷,mamicode.com
首页 > 移动开发 > 详细

Android对象序列化存储

时间:2015-07-10 18:52:18      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

序列化的对象需要实现Serializable接口或者时容器对象

    /**
     * Save the object
     *
     * @param context context
     * @param ser     serializable object
     * @param file    cache file
     * @throws java.io.IOException IOException
     */
    @SuppressWarnings("JavaDoc")
    public static boolean saveObject(Context context, Serializable ser,
                                     String file) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = context.openFileOutput(file, Context.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(ser);
            oos.flush();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (oos != null) oos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (fos != null) fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Read the object
     *
     * @param context context
     * @param file    cache file
     * @return serializable object
     * @throws java.io.IOException Exception
     */
    @SuppressWarnings("JavaDoc")
    public static Serializable readObject(Context context, String file) {
        if (!isExistDataCache(context, file))
            return null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = context.openFileInput(file);
            ois = new ObjectInputStream(fis);
            return (Serializable) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            // Delete the cache file if the deserialization failed.
            if (e instanceof InvalidClassException) {
                File data = context.getFileStreamPath(file);
                //noinspection ResultOfMethodCallIgnored
                data.delete();
            }
        } finally {
            try {
                if (ois != null) ois.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (fis != null) fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * Judge whether the cache file is readable.
     *
     * @param context   context
     * @param cachefile cache file
     * @return true if the cache file is readable, false otherwise.
     */
    public static boolean isReadDataCache(Context context, String cachefile) {
        return readObject(context, cachefile) != null;
    }

    /**
     * Juget whether the cache file exists.
     *
     * @param context   context
     * @param cachefile cache file
     * @return true if the cache data exists, false otherwise.
     */
    public static boolean isExistDataCache(Context context, String cachefile) {
        if (context == null)
            return false;
        boolean exist = false;
        File data = context.getFileStreamPath(cachefile);
        if (data.exists())
            exist = true;
        return exist;
    }

 

Android对象序列化存储

标签:

原文地址:http://www.cnblogs.com/xbx2015/p/4636422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!