Files.copy throws java.nio.file.NoSuchFileException even though the file to be copied definitely exists

8.6k views Asked by At

I have a problem with a seemingly simple application. What it should do:

-Read out the files (*.jpg) of a (hardcoded) directory

-Use the contained Metadata (gotten via implemented Libraries) of said jpgs to generate directories (./year/month/)

-copy the files into the corresponding directories.

What it doesn't: -copy the files into the corresponding directories BECAUSE it doesn't find the original files (which it read out itself previously). I honestly have no clue why that is.

Here the sourcecode:

package fotosorter;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Date;

public class Fotosorter {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws JpegProcessingException, IOException {
    File startdir = new File(System.getProperty("user.dir"));
    FileFilter jpg = new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.getAbsoluteFile().toString().toLowerCase().endsWith(".jpg");
        }
    };

    File dir = new File(startdir, "bitmaps"+File.separator+"java-temp");
    if (!(dir.exists() && dir.isDirectory())) {
        if (!dir.mkdir()) {
            throw new IOException("kann das Verzeichnis nicht erzeugen ");
        }
    }


    File[] files = new File(startdir, "" + File.separator + "bitmaps" + File.separator + "java-fotos").listFiles(jpg);
    for (File file : files) {
        Metadata metadata = JpegMetadataReader.readMetadata(file);
        ExifIFD0Directory directory = metadata.getDirectory(ExifIFD0Directory.class);
        String[] dates = directory.getDate(ExifIFD0Directory.TAG_DATETIME).toString().split(" ");

        File year = new File(dir, dates[5]);
        File month = new File(year, dates[1]);

        File fname = new File(month, file.getName());
        if (!(month.getParentFile().exists() && month.getParentFile().isDirectory())) {
            if (!month.mkdirs()) {
                throw new IOException("kann die Verzeichnisse nicht erzeugen");
            }
        }

        copyFile(file, fname);
    }
}

public static void copyFile(File from, File to) throws IOException {
    Files.copy(from.toPath(), to.toPath());
}

}

And here the full exception it throws:

run: Exception in thread "main" java.nio.file.NoSuchFileException: D:\Benutzerdaten\Paul\Documents\NetBeansProjects\Fotosorter\bitmaps\java-fotos\cimg2709.jpg -> D:\Benutzerdaten\Paul\Documents\NetBeansProjects\Fotosorter\bitmaps\java-temp\2008\Sep\cimg2709.jpg at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsFileCopy.copy(WindowsFileCopy.java:205) at sun.nio.fs.WindowsFileSystemProvider.copy(WindowsFileSystemProvider.java:277) at java.nio.file.Files.copy(Files.java:1225) at fotosorter.Fotosorter.copyFile(Fotosorter.java:64) at fotosorter.Fotosorter.main(Fotosorter.java:59) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

As you may have guessed it's not finished yet. Apart from solving my previously stated problem I still have to put it into methods.

1

There are 1 answers

0
Thorsten Niehues On

Make sure that the input file exists.

But also make sure that the path of the destination folder does exist.