Java FileOutputStream上的FileNotFoundException
我的android应用程序是非常简单的解压缩 我想创建一个文件名为的文件夹,删除zip文件中的扩展名.zip 我成功了 但是,某些设备中存在例外情况 设备名称:KM-S300 操作系统版本:2.3.4 资料来源:Java FileOutputStream上的FileNotFoundException,java,android,Java,Android,我的android应用程序是非常简单的解压缩 我想创建一个文件名为的文件夹,删除zip文件中的扩展名.zip 我成功了 但是,某些设备中存在例外情况 设备名称:KM-S300 操作系统版本:2.3.4 资料来源: private void extractZip (File file) throws UTFDataFormatException { FileInputStream fis = null; FileOutputStream fos = null; ZipInp
private void extractZip (File file) throws UTFDataFormatException {
FileInputStream fis = null;
FileOutputStream fos = null;
ZipInputStream zis = null;
ZipEntry ze = null;
byte[] data = new byte[1024];
int offset = 0;
String rootName = file.getAbsolutePath();
rootName = rootName.substring(0, rootName.lastIndexOf("."));
String rootFileName = rootName.substring(rootName.lastIndexOf("/") + 1);
File root = new File(rootName);
root.mkdirs();
try {
fis = new FileInputStream(file);
zis = new ZipInputStream(fis);
while (( ze = zis.getNextEntry() ) != null) {
try {
File f = new File(root, ze.getName());
if (!f.isDirectory()) {
f.getParentFile().mkdirs();
fos = new FileOutputStream(f); // <<-- ERROR
while (( offset = zis.read(data) ) != -1) {
fos.write(data, 0, offset);
}
}
} finally {
try {
fos.close();
} catch (Exception e) {}
}
}
file.delete();
}catch (UTFDataFormatException e){
throw e;
}catch (Exception e) {
e.printStackTrace();
} finally {
try {
zis.close();
} catch (Exception e) {}
try {
fis.close();
} catch (Exception e) {}
}
}
我不能理解例外
java.io.FileNotFoundException: /mnt/sdcard/test/marker/Explosion/failed/0.png (No such file or directory)
at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
at com.sample.MainActivity.extractZip(MainActivity.java:507)
在创建FileOutputStream之前创建到文件的父文件夹
为什么应用程序会出现异常?仅当新文件不存在或不是目录时,才创建父目录。这不是一个明智的测试。如果父目录不存在,您应该创建它。它是否与FileNotFoundException相关??由于异常,临时添加了mkdirs。但问题仍然存在。图特