4

我有一个看似简单的应用程序的问题。它应该做什么:

-读出(硬编码)目录的文件(*.jpg)

- 使用所述jpg的包含的元数据(通过实施的库获得)生成目录(./year/month/)

-将文件复制到相应的目录中。

它没有: - 将文件复制到相应的目录中,因为它没有找到原始文件(它之前自行读出)。老实说,我不知道为什么会这样。

这里是源代码:

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

}

这里是它抛出的完整异常:

运行:线程“主”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 在 sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79) 在 sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)在 sun.nio.fs.WindowsFileCopy.copy(WindowsFileCopy.java:205) 在 sun.nio.fs.WindowsFileSystemProvider.copy(WindowsFileSystemProvider.java:277) 在 java.nio.file.Files.copy(Files.java:1225) ) 在 fotosorter.Fotosorter.copyFile(Fotosorter.java:64) 在 fotosorter.Fotosorter.main(Fotosorter.java:59) Java 结果:1 BUILD SUCCESSFUL(总时间:0 秒)

正如您可能已经猜到的那样,它还没有完成。除了解决我之前提到的问题外,我还必须将其放入方法中。

4

1 回答 1

2

确保输入文件存在。

但还要确保目标文件夹的路径确实存在。

于 2017-06-14T11:25:36.753 回答