简体   繁体   English

FileOutputStream上的FileNotFoundException

[英]FileNotFoundException on FileOutputStream

My android application is very simply extract zip. 我的android应用程序非常简单地提取zip。

I want to create a folder with the FileName erase the extension (.zip) in the zip file. 我想用FileName创建一个文件夹,在zip文件中删除扩展名(.zip)。

And i'm succeeded. 我成功了。

However, the exception in some devices. 但是,某些设备例外。

device name : KM-S300 os version : 2.3.4 设备名称:KM-S300操作系统版本:2.3.4

source: 资源:

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) {}
    }
}

I can't understand Exception 我无法理解异常

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)

Made to the parent folder of the File prior to creating the FileOutputStream 在创建FileOutputStream之前对文件的父文件夹进行

Why did exception by application? 为什么按应用程序例外?

You're only creating the parent directory if the new file doesn't exist or isn't a directory. 如果新文件不存在或者不是目录,则只创建父目录。 It's not a sensible test. 这不是一个明智的考验。 You should create it if the parent directory doesn't exist. 如果父目录不存在,您应该创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM