FileNotFoundException(无此类文件或目录) [英] FileNotFoundException (no such file or directory)

查看:98
本文介绍了FileNotFoundException(无此类文件或目录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个android应用程序,我需要从几个文件夹中读取几个文件,并将它们添加到几个zip存档中.我需要将档案的最大大小限制为16mb.因此,在运行时,如果文件大小超过16 mb,则将文件添加到存档中时,请创建另一个具有相同大小限制的存档,依此类推.我正在使用以下包装器类:

I'm writing an android app and I need to read several files from several folders and add them to several zip archives. I need to limit the max size of the archives to lets say 16mb. So at runtime while adding the files to the archive if the size of it exceeds 16 mb create another archive with the same size limit and so on. I'm using the following wrapper class:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ChunkedZippedOutputStream {
    private ZipOutputStream zipOutputStream;

    private String path;

    private String name;

    private long currentSize;

    private int currentChunkIndex;

    private final long MAX_FILE_SIZE = 16 * 1000 * 1024; // 16mb limit

    private final String PART_POSTFIX = ".part";

    private final String FILE_EXTENSION = ".zip";

    public ChunkedZippedOutputStream(String path, String name) throws FileNotFoundException {
        this.path = path;
        this.name = name;
        constructNewStream();
    }

    public void addEntry(ZipEntry entry) throws IOException {
        long entrySize = entry.getCompressedSize();
        if ((currentSize + entrySize) > MAX_FILE_SIZE) {
            closeStream();
            constructNewStream();
        } else {
            currentSize += entrySize;
            zipOutputStream.putNextEntry(entry);
        }
    }

    private void closeStream() throws IOException {
        zipOutputStream.close();
    }

    private void constructNewStream() throws FileNotFoundException {
        zipOutputStream = new ZipOutputStream(new FileOutputStream(new File(path, constructCurrentPartName())));
        currentChunkIndex++;
        currentSize = 0;
    }

    private String constructCurrentPartName() {
        // This will give names is the form of <file_name>.part.0.zip, <file_name>.part.1.zip, etc.
        StringBuilder partNameBuilder = new StringBuilder(name);
        partNameBuilder.append(PART_POSTFIX);
        partNameBuilder.append(currentChunkIndex);
        partNameBuilder.append(FILE_EXTENSION);
        return partNameBuilder.toString();
    }
}

我这样使用它:

String zipPath = Environment.getExternalStorageDirectory() + "/MyApp/MyFolder/Zip/";
String zipName = "MyZipFle";
ChunkedZippedOutputStream zippedOutputStream = new ChunkedZippedOutputStream(zipPath, zipName);
....
zippedOutputStream.addEntry(new ZipEntry("ZipEntry" + i));

但是ChunkedZippedOutputStream对象的实例化却出现此错误:

but an instantiation of the ChunkedZippedOutputStream object I get this error:

  java.io.FileNotFoundException: /mnt/sdcard/MyApp/MyFolder/Zip/MyZipFle.part0.zip (No such file or directory)

我知道我在输入路径或输入名称时做错了什么,但我不知道该怎么办.

I know I'm doing something wrong with the path input or the name but I can't figure it out what.

如果代码段不正确,请告诉我,我是从这里

Also if the code snippet is not correct please tell me, I got it from here How to split a huge zip file into multiple volumes?

如果对我的问题有更简单的解决方案,请告诉我.谢谢

If there is a simpler solution to my problem please tell me. Thank you

推荐答案

输出目录不存在.有关解决方案,请参见File.mkdirs().

The output directory doesn't exist. See File.mkdirs() for the solution.

这篇关于FileNotFoundException(无此类文件或目录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆