没有找到获取文件例外 [英] getting file not found exception
本文介绍了没有找到获取文件例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的android活动:
I have my android activity :
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异常: 九月1日至13日:06:44.442:WARN / System.err的(419):java.io.FileNotFoundException:/mnt/sdcard/downloads/mytext.txt(没有这样的文件或目录)
but it throws me FileNotfound exception : 01-13 09:06:44.442: WARN/System.err(419): java.io.FileNotFoundException: /mnt/sdcard/downloads/mytext.txt (No such file or directory)
如果我添加
if(file.exists()){
System.out.println("file exists");
}
else{
System.out.println("No such Fileeeeeeeeee");
}
它移动到其他的一部分。
it moves into "else" part.
感谢
Sneha
Thanks
Sneha
推荐答案
试试这个,,它为我的作品
Try this,,it works for me
// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
//now attach OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
<一个href="http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29">GO通过此了解更多详情
这篇关于没有找到获取文件例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文