Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileNotFoundException (no such file or directory)

Tags:

java

java-io

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();
    }
}

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:

  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

like image 253
androidu Avatar asked Jun 18 '12 14:06

androidu


People also ask

How do I fix FileNotFoundException?

How to Fix FileNotFoundException. Since FileNotFoundException is a checked exception, a try-catch block should be used to handle it. The try block should contain the lines of code that can throw the exception and the catch block should catch and handle the exception appropriately.

What is FileNotFoundException in Java?

Class FileNotFoundException Signals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the FileInputStream , FileOutputStream , and RandomAccessFile constructors when a file with the specified pathname does not exist.

Is FileNotFoundException a runtime exception?

FileNotFoundException occurs at runtime so it is a checked exception, we can handle this exception by java code, and we have to take care of the code so that this exception doesn't occur.


1 Answers

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

like image 107
user207421 Avatar answered Oct 01 '22 03:10

user207421