【发布时间】:2012-01-13 03:28:17
【问题描述】:
我有我的安卓活动:
try {
File root=Environment.getExternalStorageDirectory();
Log.i("root",root.toString());
File dir=new File(root.getAbsolutePath() + "/downloads");
dir.mkdirs();
file=new File(dir,"mytext.txt");
FileOutputStream out=new FileOutputStream(file,true);
PrintWriter pw=new PrintWriter(out);
pw.println("Hello! Welcome");
pw.println("You are Here...!!!");
pw.flush();
pw.close();
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
还添加了:
<uses-permission android:name="androd.permission.WRITE_EXTERNAL_STORAGE"/>
但它会抛出 FileNotfound 异常: 01-13 09:06:44.442: WARN/System.err(419): java.io.FileNotFoundException: /mnt/sdcard/downloads/mytext.txt(没有这样的文件或目录)
如果我添加
if(file.exists()){
System.out.println("file exists");
}
else{
System.out.println("No such Fileeeeeeeeee");
}
它进入“其他”部分。
谢谢
斯内哈
【问题讨论】:
-
您必须使用 getExternalStorageState() 方法检查媒体是否可用。
-
可用,可读可写。
-
你能解释一下你在用“dir.mkdirs()”做什么吗?
-
创建一个包含sdcard完整路径的新目录
标签: android filenotfoundexception