FileNotFoundException异常(没有这样的文件或目录)(FileNotFoundEx

2019-09-18 05:17发布

我正在写一个Android应用程序,我需要从多个文件读取多个文件,并将它们添加到几个ZIP档案。 我需要限制档案的最大尺寸可以说16MB。 因此,在运行时,在添加文件到压缩文件,如果它的大小超过16 MB创建具有相同大小限制等其他存档。 我用下面的包装类:

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对象的实例我得到这个错误:

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

我知道我在做什么毛病路径输入或名字,但我无法弄清楚什么。

此外,如果该代码段是不正确的,请告诉我,我是从这里如何将巨大的zip文件拆分为多个卷?

如果有一个更简单的解决我的问题,请告诉我。 谢谢

Answer 1:

输出目录不存在。 用于溶液看到File.mkdirs(),



Answer 2:

这为我工作

Project
|->src
|   |-->MyClass.java
|   |-->MyFile1.txt
|->res
   |->files
     |-->MyFile2.txt

例:

为MyFile1,您可以使用新的文件(“MyFile1.txt”);
为MyFile2,您可以使用新的文件( “./ RES /文件/ MyFile2.txt”);



文章来源: FileNotFoundException (no such file or directory)
标签: java java-io