您的位置:首页 > 移动开发 > Android开发

Android文件存储小结

2016-10-12 10:04 267 查看
核心技术就是Context类的openFileInput()和openFileOutput()方法,之后就是利用java的各种流进行读写操作。但是文件存储并不适用于保存一些比较复杂的文本数据。

典型结构:

/*存储*/
FileOutputStream out=null;
PrintWriter pw=null;
try{
out=openFileOutput("data",Context.MODE_PRIVATE);
//可以看出openFileOutput方法返回的是FileOutputStream类型
pw=new PrintWriter(out);
pw.write(inputText);
pw.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(writer!=null){
writer.close();
}
}catch(IOException e){
e.printStackTrace();
}
}


/*读取*/
FileInputStream in=null;
BufferedReader br=null;
StringBuilder sb=new StringBuilder();
try{
in=openFileInput("data");
br=new BufferedReader(new InputSreamReader(in));
String temp="";
if((temp=br.readLine())!=null){
sb.append(temp);
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(br!=null){
try{
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 存储