【发布时间】:2014-02-17 11:13:23
【问题描述】:
我有一个看似简单的应用程序的问题。 它应该做什么:
-读出(硬编码)目录的文件(*.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 构建成功(总时间:0 秒)
您可能已经猜到它还没有完成。除了解决我之前提到的问题,我还得把它放到方法中。
【问题讨论】:
-
D:\Benutzerdaten\Paul\Documents\NetBeansProjects\Fotosorter\bitmaps\java-temp\2008 是否存在?为什么只有当月份的父文件不存在时才调用 mkdirs() ,而不是在月份本身不存在时才调用?
-
最近的编辑表明,这个网站不需要礼貌。此外,实际的问题,或者更确切地说是“请求帮助和启发”被大胆地删除了。我希望这不会影响我的问题得到解决的机会。
-
不,它没有。但回答 cmets 中提出的问题将有助于获得答案。
-
是的,当然。我只需要一点时间来测试一下。是的,你确实是对的。我一定忽略了这一点,它完美地解决了我的问题。很抱歉打扰您,感谢您的快速解决方案。
标签: java file copy nosuchfileexception