android 修改doc文件内容,android 读取文件内容操作.doc

本文介绍了Android系统中如何读取和写入文件,重点展示了使用`context.openFileInput()`和`context.openFileOutput()`方法进行操作。代码示例演示了读取和写入文件的实现,同时提到了权限控制和文件共享的概念。此外,还讨论了将数据存储为Properties对象的可能性。
摘要由CSDN通过智能技术生成

android 读取文件内容操作

android 读取文件内容操作

在Android系统中,这些文件保存在 /data/data/PACKAGE_NAME/files 目录下。?数据读取?view plaincopy to clipboardprint??public static String read(Context context, String file) {?String data = "";?try {?FileInputStream stream = context.openFileInput(file);?StringBuffer sb = new StringBuffer();?int c;?while ((c = stream.read()) != -1) {?sb.append((char) c);?}?stream.close();?data = sb.toString();?} catch (FileNotFoundException e) {?} catch (IOException e) {?}?return data;?}?public static String read(Context context, String file) {?String data = "";?try {?FileInputStream stream = context.openFileInput(file);?StringBuffer sb = new StringBuffer();?int c;?while ((c = stream.read()) != -1) {?sb.append((char) c);?}?stream.close();?data = sb.toString();?} catch (FileNotFoundException e) {?} catch (IOException e) {?}?return data;?}?从代码上,看起来唯一的不同就是文件的打开方式了: context.openFileInput(file); Android中的文件读写具有权限控制,所以使用context(Activity的父类)来打开文件,文件在相同的Package中共享。这里的 Package的概念同Preferences中所述的Package,不同于Java中的Package。?数据写入?view plaincopy to clipboardprint??public static void write(Context context, String file, String msg) {?try {?FileOutputStream stream = context.openFileOutput(file,?Context.MODE_WORLD_WRITEABLE);?stream.write(msg.getBytes());?stream.flush();?stream.close();?} catch (FileNotFoundException e) {?} catch (IOException e) {?}?}?public static void write(Context context, String file, String msg) {?try {?FileOutputStream stream = context.openFileOutput(file,?Context.MODE_WORLD_WRITEABLE);?stream.write(msg.getBytes());?stream.flush();?stream.close();?} catch (FileNotFoundException e) {?} catch (IOException e) {?}?}?在这里打开文件的时候,声明了文件打开的方式。?一般来说,直接使用文件可能不太好用,尤其是,我们想要存放一些琐碎的数据,那么要生成一些琐碎的文件,或者在同一文件中定义一下格式。其实也可以将其包装成Properties来使用:?view plaincopy to clipboardprint??public static Properties load(Context context, String file) {?Properties properties = new Properties();?try {?FileInputStream stream = context.openFileInput(fi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值