您的位置:首页 > 其它

安卓 文件读写

2016-07-22 15:18 405 查看
1,判断文件是否存在

public static boolean fileIsExists(String filePath){
try{
File f=new File(filePath);
if(!f.exists()){
return false;
}
}catch (Exception e) {
// TODO: handle exception
return false;
}
return true;
}

2,写入字符串

//写入文件
public static void writeFile(String fileName, String message){

if (fileIsExists(fileName)) {
try {
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
byte [] bytes = message.getBytes();

fileOutputStream.write(bytes);
fileOutputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

注意:别忘了mainfest.xml添加权限

  <!-- 操作sd卡权限 -->

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

如果报错提示:java.io.FileNotFoundException: /proc/hanvon_emp/pen_mode: open failed: EACCES (Permission denied),检查文件属性是否可读写!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: