我得到这个错误获得FileNotFoundException异常/ EISDIR
java.io.FileNotFoundException: /data/data/com.example.app/cache/news.xml:打开失败: EISDIR(是一个目录)
使用此代码
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();
}
更新添加代码删除与所需文件名匹配的目录(f any)+正确使用mkdirs似乎解决了这个问题。接受最接近的答案。
您是否声明了'WRITE_EXTERNAL_STORAGE'权限? –
的路径是否包含文件名? – giorashc
@giorasch是的它确实 – Tom