Question

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)
Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top