Tom*_*Tom 3 java android exception file
我收到这个错误
java.io.FileNotFoundException:/data/data/com.example.app/cache/news.xml:open failed: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();
}
Run Code Online (Sandbox Code Playgroud)
更新添加代码以删除与所需文件名匹配的目录(f any)+正确使用mkdirs似乎已解决了问题.接受最接近的答案.
mkdirs()
不仅会创建指向文件的目录,还会创建包含文件指向的路径的目录.这就是createNewFile()
失败的原因.您需要调用mkdirs()
父文件:
File parent = f.getParentFile();
if (parent != null) parent.mkdirs();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5577 次 |
最近记录: |