문제

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