写入文件

public static boolean saveObject(Context context, Serializable serializable, String file) {        FileOutputStream fos = null;        ObjectOutputStream oos = null;        try {            fos = context.openFileOutput(file, Context.MODE_PRIVATE);            oos = new ObjectOutputStream(fos);            oos.writeObject(serializable);            oos.flush();            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        } finally {            try {                oos.close();            } catch (IOException e) {                e.printStackTrace();            }            try {                fos.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }

读取文件

public static Serializable readObject(Context context, String file) {        FileInputStream fis = null;        ObjectInputStream ois = null;        try {            fis = context.openFileInput(file);            ois = new ObjectInputStream(fis);            return (Serializable) ois.readObject();        } catch (Exception e) {            e.printStackTrace();            if(e instanceof InvalidClassException){                File data = context.getFileStreamPath(file);                data.delete();            }        } finally {            try {                ois.close();            } catch (IOException e) {                e.printStackTrace();            }            try {                fis.close();            } catch (IOException e) {                e.printStackTrace();            }        }        return null;    }

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. android之sharedpreference读取参数
  4. Android手机拍照和手机相册选取图片的工具类ImageUtils
  5. android之File
  6. Android(安卓)Ant打包及其环境变量配置
  7. Android(安卓)时区的修改增删,如何在菜单里增加一个没有的时区
  8. android unity 文件读写_unity Android(安卓)打包后读取 xml 文
  9. 安卓开发遇坑记录

随机推荐

  1. Android引路蜂地图开发示例:基本知识
  2. Android开机阶段log分析
  3. 安卓样式之shape(形状)
  4. 在代码中实现android:tint效果
  5. 【Android 开发】: Android 消息处理机制
  6. Android SELinux 概览(转自官网)
  7. Android的系统构架
  8. 十六周总结报告
  9. Android布局优化(二)优雅获取界面布局耗时
  10. RelativeLayout布局的对齐属性