FileInputStream FileOutputStream
Context提供了如下两个方法来打开应用程序的数据文件夹里的文件IO流
1).FileInputStream openFileInput(String name):打开应用程序的数据文件夹下的name文件对应的输入流
2).FileOutputStream openFileOutput(String name, int mode):打开应用程序的数据文件夹下的name文件对应的输出流
第二个方法的第二个参数为指定文件的打开模式:
MODE_PRIVATE:该文件只能被当前的程序读写
MODE_APPEND:以追加方式打开该文件,应用程序可以向该文件中追加内容
MODE_WORLD_READABLE:该文件的内容可以被其他程序读取
MODE_WORLD_WRITEABLE:该文件的内容可由其他程序读写
Android 4.2开始,android 不推荐使用MODE_WORLD_READABLE、MODE_WORLD_WRITEABLE.
private String read(){
//打开文件输入流
try {
FileInputStream fis =openFileInput(“zhaoyi”);
byte [] buff =new byte[1024];
int hasRead=0;
StringBuilder sb=new StringBuilder(“”);
//读取文件内容
while ((hasRead=fis.read(buff))>0) {
sb.append(new String(buff, 0, hasRead));
}
fis.close();
Log.i("zhaoyi11","sb "+sb.toString());
return sb.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private void write(String content){
Log.i("zhaoyi11","content "+content);
//以追加方式打开文件输出流
try {
FileOutputStream fos=openFileOutput("zhaoyi", MODE_APPEND);
//将FileOutputStream 包装成PrintStream
PrintStream pS=new PrintStream(fos);
//输出文件内容
pS.println(content);
pS.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}