简体   繁体   English

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

[英]FileNotFoundException (no such file or directory)

I'm writing an android app and I need to read several files from several folders and add them to several zip archives.我正在编写一个 android 应用程序,我需要从几个文件夹中读取几个文件并将它们添加到几个 zip 档案中。 I need to limit the max size of the archives to lets say 16mb.我需要将档案的最大大小限制为 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.因此,在运行时将文件添加到存档时,如果它的大小超过 16 mb,则创建另一个具有相同大小限制的存档,依此类推。 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();
    }
}

and I use it like this:我像这样使用它:

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

but an instantiation of the ChunkedZippedOutputStream object I get this error:但是在 ChunkedZippedOutputStream 对象的一个​​实例化我得到这个错误:

  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?另外,如果代码片段不正确,请告诉我,我从这里得到它如何将一个巨大的 zip 文件拆分为多个卷?

If there is a simpler solution to my problem please tell me.如果我的问题有更简单的解决方案,请告诉我。 Thank you谢谢

The output directory doesn't exist.输出目录不存在。 See File.mkdirs() for the solution.有关解决方案,请参阅File.mkdirs()

This worked for me这对我有用

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

Example:例子:

for MyFile1, you can use new File("MyFile1.txt");对于 MyFile1,您可以使用 new File("MyFile1.txt");
for MyFile2, you can use new File("./res/files/MyFile2.txt");对于 MyFile2,您可以使用 new File("./res/files/MyFile2.txt");

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

相关问题 Spring FileNotFoundException ..(没有此类文件或目录) - Spring FileNotFoundException .. (No such file or directory) 我知道的文件的FileNotFoundException在目录中 - FileNotFoundException for a file I know is in the directory IntelliJ IDEA文件FileNotFoundException,目录和文件是否存在? - IntelliJ IDEA File FileNotFoundException, directory and file exists? java.io.FileNotFoundException:..(无此类文件或目录) - java.io.FileNotFoundException: .. (No such file or directory) FileNotFoundException 打开失败:ENOENT(没有这样的文件或目录) - FileNotFoundException open failed: ENOENT (No such file or directory) java.io.FileNotFoundException:没有这样的文件或目录错误 - java.io.FileNotFoundException: No such file or directory Error java.io.FileNotFoundException:[文件路径](没有这样的文件或目录) - java.io.FileNotFoundException: [filepath] (No such file or directory) FileNotFoundException(不是目录) - FileNotFoundException (Not a directory) java.io.FileNotFoundException没有此类文件或目录错误,但目录存在 - java.io.FileNotFoundException No such file or directory error but directory exists 从 config.file 读取抛出 FileNotFoundException(没有这样的文件或目录) - Reading from config.file throws FileNotFoundException (no such file or directory)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM