5

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.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
user3026231
  • 51
  • 1
  • 2
  • Does D:\Benutzerdaten\Paul\Documents\NetBeansProjects\Fotosorter\bitmaps\java-temp\2008 exist? Why do you call mkdirs() only if the parent file of month doesn't exist, and not if month itself doesn't exist? – JB Nizet Jan 26 '14 at 17:47
  • Recent edits suggest, politeness is not wanted on this site. Also, the actual question, or rather "plea for help and inspiration" was boldly removed. I hope that does not affect the chances of my problem getting solved. – user3026231 Jan 26 '14 at 17:50
  • 1
    No, it doesn't. But answering to questions asked in comments will help in getting an answer. – JB Nizet Jan 26 '14 at 17:52
  • Yes, of course. I needed just a little time testing the matter. And yes indeed you were right. I must have overlooked that and it solved my problem perfectly. I am sorry for bothering you and thankful for the quick solution. – user3026231 Jan 26 '14 at 17:56

1 Answers1

2

Make sure that the input file exists.

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

Thorsten Niehues
  • 13,712
  • 22
  • 78
  • 113