最进在写一个应用需要保存文件到SD卡上,可是老是会出错,在网上查了许多资料也没有解决,研究了一个下午,终于发现问题,在此记下方便以后查阅,至于其中的原理,大神路过可以解释一下
//新建文件夹
File file=new File("/sdcard/notepad");
if(!file.exists()){file.mkdirs();
}
//新建文件
File f=new File(file.getAbsolutePath()+"/log.txt");f.createNewFile();
FileOutputStream osw=new FileOutputStream(f,true);
String s="hello world!";
osw.write(s.getBytes());osw.flush();
osw.close();
会出现的错误:
Not such file or directory;
creatNewFile报错等,那一定是你权限没加或者写错了(肯定是这样),最好再粘一份
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>