2014-03-03 159 views
0

我裏面的SD卡Android的子文件夾的SD卡

File f = new File(Environment.getExternalStorageDirectory() + "/res/"); 
f.mkdirs(); 

成功創建該文件夾創建一個名爲「資源」文件夾中。

但是,當我嘗試在文件夾內創建文件不起作用。

String string = "hello!"; 

File file = new File(Environment.getExternalStorageDirectory() + "/res/","test.txt"); 
FileOutputStream fos = new FileOutputStream(file); 
fos.write(string.getBytes()); 
fos.close(); 

logcat的

java.io.FileNotFoundException: /mnt/sdcard/res/test.txt (Not a directory) 
+0

只需調用'file.createNewFile();'第一個 –

回答

0

到​​調用並不實際創建該文件。您需要使用file.exists()來檢查文件是否存在。如果沒有,則必須通過調用file.createNewFile()來創建該文件。請參閱此答案的示例代碼:https://stackoverflow.com/a/7012122/379245