免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1920 | 回复: 0
打印 上一主题 下一主题

android 读取文件内容操作 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-30 11:39 |只看该作者 |倒序浏览
android 读取文件内容操作








在Android系统中,这些文件保存在 /data/data/PACKAGE_NAME/files 目录下。

数据读取
  1. view plaincopy to clipboardprint?
  2. public static String read(Context context, String file) {
  3. String data = "";
  4. try {
  5. FileInputStream stream = context.openFileInput(file);
  6. StringBuffer sb = new StringBuffer();
  7. int c;
  8. while ((c = stream.read()) != -1) {
  9. sb.append((char) c);
  10. }
  11. stream.close();
  12. data = sb.toString();

  13. } catch (FileNotFoundException e) {
  14. } catch (IOException e) {
  15. }
  16. return data;
  17. }
  18. public static String read(Context context, String file) {
  19. String data = "";
  20. try {
  21. FileInputStream stream = context.openFileInput(file);
  22. StringBuffer sb = new StringBuffer();
  23. int c;
  24. while ((c = stream.read()) != -1) {
  25. sb.append((char) c);
  26. }
  27. stream.close();
  28. data = sb.toString();

  29. } catch (FileNotFoundException e) {
  30. } catch (IOException e) {
  31. }
  32. return data;
  33. }
复制代码
从代码上,看起来唯一的不同就是文件的打开方式了: context.openFileInput(file); Android中的文件读写具有权限控制,所以使用context(Activity的父类)来打开文件,文件在相同的Package中共享。这里的 Package的概念同Preferences中所述的Package,不同于Java中的Package。

数据写入
  1. view plaincopy to clipboardprint?
  2. public static void write(Context context, String file, String msg) {
  3. try {
  4. FileOutputStream stream = context.openFileOutput(file,
  5. Context.MODE_WORLD_WRITEABLE);
  6. stream.write(msg.getBytes());
  7. stream.flush();
  8. stream.close();
  9. } catch (FileNotFoundException e) {
  10. } catch (IOException e) {
  11. }
  12. }
  13. public static void write(Context context, String file, String msg) {
  14. try {
  15. FileOutputStream stream = context.openFileOutput(file,
  16. Context.MODE_WORLD_WRITEABLE);
  17. stream.write(msg.getBytes());
  18. stream.flush();
  19. stream.close();
  20. } catch (FileNotFoundException e) {
  21. } catch (IOException e) {
  22. }
  23. }
复制代码
在这里打开文件的时候,声明了文件打开的方式。

一般来说,直接使用文件可能不太好用,尤其是,我们想要存放一些琐碎的数据,那么要生成一些琐碎的文件,或者在同一文件中定义一下格式。其实也可以将其包装成Properties来使用:
  1. view plaincopy to clipboardprint?
  2. public static Properties load(Context context, String file) {
  3. Properties properties = new Properties();
  4. try {
  5. FileInputStream stream = context.openFileInput(file);
  6. properties.load(stream);
  7. } catch (FileNotFoundException e) {
  8. } catch (IOException e) {
  9. }
  10. return properties;
  11. }

  12. public static void store(Context context, String file, Properties properties) {
  13. try {
  14. FileOutputStream stream = context.openFileOutput(file,
  15. Context.MODE_WORLD_WRITEABLE);
  16. properties.store(stream, "");
  17. } catch (FileNotFoundException e) {
  18. } catch (IOException e) {
  19. }
  20. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP