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(file);
properties.load(stream);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return properties;
}

public static void store(Context context, String file, Properties properties) {
try {
FileOutputStream stream = context.openFileOutput(file,
Context.MODE_WORLD_WRITEABLE);
properties.store(stream, "");
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}

android 读取文件内容操作2

读:
public String ReadSettings(Context context){
FileInputStream fIn = null;
InputStreamReader isr = null;

char[] inputBuffer = new char[255];
String data = null;

try{
fIn = openFileInput("settings.dat");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
}
finally {
try {
isr.close();
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}

写:
public void WriteSettings(Context context, String data){
FileOutputStream fOut = null;
OutputStreamWriter osw = null;

try{
fOut = openFileOutput("settings.dat",MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
osw.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

使用方法:
WriteSettings(this,"setting0, setting1, setting2");
String data[] = ReadSettings(this).split(",");

更多相关文章

  1. android 竖屏activity跳转横屏activity返回时数据消失
  2. js 判断当前操作系统 ios, android, 电脑端
  3. Android中进行HTTP操作
  4. Android文件浏览器的开发
  5. android布局文件中的include
  6. android中ListView控件&&onItemClick事件中获取listView传递的数
  7. Android上传文件到Web服务器,PHP接收文件(二)
  8. 在非主线程中不能操作主线程中的View

随机推荐

  1. retrofit2+okhttp3+rxjava网络封装
  2. Android(安卓)Studio导入项目的几个问题
  3. android Activity的onPause()与onResume(
  4. android 调用百度地图apk应用的导航功能
  5. Android(安卓)EditText 禁止换行
  6. Android(安卓)短信模块分析(三) MMS入口
  7. Android(安卓)EventBus学习
  8. 小谈Activity启动模式
  9. 仿抖音短视频APP源码,android获取屏幕尺寸
  10. 一文教你实现 SpringBoot 中的自定义 Val