Android存储(读取)之内存FilesDir存储
2016-05-15 11:24
411 查看
数据存储到私有文件夹中:data/data/包名/files/
点击“清除数据”按钮可以删除data/data/包名/files/下的数据
点击“清除数据”按钮可以删除data/data/包名/files/下的数据
// 把String保存到私有文件夹中:data/data/包名/files/ StoreUtils.storeStringToFilesDir(this, "fileDir", "ha.txt"); //读取data/data/包名/files/中的文件 String data = StoreUtils.readStringFromFilesDir(new File(getFilesDir(), "ha.txt"));
/** * 把数据存储到私有文件夹中 * 默认目录:data/data/包名/files/ * 1.同样上面的目录可以使用context.getFilesDir()来获取 * 2.只有自己的应用程序可以访问该目录,其他应用程序无法访问该目录(私有文件夹) * 真实的手机data这个目录是打不开的(未root),这是为了保护隐私,系统做了权限设置 */ /** * 数据存储到私有文件夹中:data/data/包名/files/ * @param context 上下文 * @param content 要保存的内容Sting * @param fileName 保存内容的文件名称 */ public static void storeStringToFilesDir(Context context, String content, String fileName) { try { File file = new File(context.getFilesDir(), fileName); FileOutputStream fos = new FileOutputStream(file); fos.write(content.getBytes()); fos.close(); Toast.makeText(context, "存储数据到FilesDir成功", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(context, "存储数据到FilesDir失败", Toast.LENGTH_SHORT).show(); } } /** * 从私有文件夹中数读取据:data/data/包名/files/ * @param file data/data/包名/files/下的文件 * @return String内容 */ public static String readStringFromFilesDir(File file) { try { FileInputStream fis = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String content = ""; String tmp; while ((tmp = br.readLine()) != null) { content += tmp; } br.close(); fis.close(); return content; } catch (FileNotFoundException e) { e.printStackTrace(); return "不存在此文件,请核对文件路径、文件名"; } catch (Exception e) { e.printStackTrace(); return "文件读取失败"; } }
相关文章推荐
- 快速集成android实现listview的字母A-Z排序,界面侧边字母索引
- Android的Selector与Shape
- RadioButton和CheckBox
- android开发笔记之sh脚本
- android 比较靠谱的图片压缩
- android开发笔记之多媒体—MediaPlayer + SurfaceView播放视频
- 浅谈android中加载高清大图及图片压缩方式(二)
- Android数据存储之SharedPreferences及如何安全存储
- Android Activity的生命周期与异常生命周期的分析
- Android Studio安装过程中出现Failed to install Intel HAXM问题及解决方法
- Android之SharedPreferences存储数据
- android LayoutInflater和inflate()方法的用法
- ScrollView嵌套ListView
- android圆角按钮自定义
- android开发笔记之多媒体—SurfaceView
- android:descendantFocusability
- 二:侧滑菜单的实现
- android:textIsSelectable
- Android 开源项目浅读-------SwipeMenuListView-重写,第四章,修改适配器
- 【Android性能优化】使用NDK进行Java和C++混编