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)
Was it helpful?

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top