安卓读写文件和路劲注意事项

写文件:

private void writeToLocal(String filesString,String content,int mode) {
try {
FileOutputStream fisStream=openFileOutput(filesString,mode);
fisStream.write(content.getBytes());
fisStream.flush();
fisStream.close();
} catch (Exception e) {
// TODO: handle exception
}
}

读文件:

private boolean readFile(String string) {
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(basicPaString+string)));
String valueString= reader.readLine();
return true;
} catch (Exception e) {
return false;
}
}

路径注意问题:

Environment.getExternalStorageDirectory()为sd卡路劲

如果读写文件时直接写文件名,则相当于this.getFilesDir()+"/"+文件名。路径为/data/data/包名/files/文件名

为了保护用户隐私,在读取和写入SD卡信息的时候,运行在4.0系统以上的系统需要增加SD卡的读取和写入权限。

文件的读写权限请看下图

 

posted on 2015-07-12 03:28  WeiLight  阅读(252)  评论(0编辑  收藏  举报