I create a folder called "res" inside sdcard

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

The folder is successfully created.

But when I try to create a file within the folder does not work.

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)
有帮助吗?

解决方案

The call to new File() doesn't actually create the file. You need to check if the file exists by using file.exists(). If it doesn't, you must create the file by calling file.createNewFile(). See this answer for sample code: https://stackoverflow.com/a/7012122/379245

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top