Android子文件夹sdcard

时间:2014-03-03 21:59:55

标签: android

我在sdcard

中创建一个名为“res”的文件夹
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)

1 个答案:

答案 0 :(得分:0)

new File()的调用实际上并不创建该文件。您需要使用file.exists()检查文件是否存在。如果没有,则必须通过调用file.createNewFile()来创建文件。有关示例代码,请参阅此答案:https://stackoverflow.com/a/7012122/379245