Android(安卓)文件操作
16lz
2021-12-04
写入文件
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; }
更多相关文章
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- android之sharedpreference读取参数
- Android手机拍照和手机相册选取图片的工具类ImageUtils
- android之File
- Android(安卓)Ant打包及其环境变量配置
- Android(安卓)时区的修改增删,如何在菜单里增加一个没有的时区
- android unity 文件读写_unity Android(安卓)打包后读取 xml 文
- 安卓开发遇坑记录