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 的父类)来打开文件,文