Smi*_*tha 4 android filenotfoundexception
我有我的android活动:
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();
}
Run Code Online (Sandbox Code Playgroud)
还补充说:
<uses-permission android:name="androd.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)
但它抛出我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");
}
Run Code Online (Sandbox Code Playgroud)
它进入"其他"部分.
谢谢
Sneha
试试这个,它对我有用
// 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20943 次 |
| 最近记录: |