I get this error
java.io.FileNotFoundException: /data/data/com.example.app/cache/news.xml: open failed: EISDIR (Is a directory)
using this code
try {
File cache = ctx.getCacheDir();
String s = cache.getAbsolutePath() + File.separator + path;
File f = new File(s);
File pf = f.getParentFile();
if (pf != null) {
pf.mkdirs();
}
if ( (pf.exists()) && (pf.isDirectory()) ) {
if ( (!f.exists()) || (!f.isFile()) ) {
f.createNewFile();
}
if ( (f.exists()) || (f.isFile()) ) {
FileOutputStream os = null;
os = new FileOutputStream(s, false);
if (os != null) {
SharedCode.sharedWriteTextFileToStream(str, os);
}
os.flush();
os.close();
}
}
}
catch (IOException e) {
String s = e.toString();
}
Update Adding code to delete the directory (f any) matching the wanted file name + correct usage of mkdirs appears to have solved the problem. Accepted closest answer.
mkdirs() creates not only the directories which lead to the file, but also a directory with the path the file points to. That's why the createNewFile() fails. You need to call mkdirs() on the parent file instead:
File parent = f.getParentFile();
if (parent != null) parent.mkdirs();
please note
f.mkdirs();
you need to check the return value of this statement. If true then proceed otherwise path is not existing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With