2014-01-26 152 views
3

我看起来很简单的应用程序有问题。 它应该做的:Files.copy抛出java.nio.file.NoSuchFileException,即使要复制的文件肯定存在

- 读取出来(硬编码)目录

- 使用包含的元数据文件(* .JPG)(通过实施库得到)的JPG文件说,生成目录(./年/月/)

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

它什么都没有: - 将文件复制到相应的目录因为它没有找到原来的文件(它以前读出它自己)。我真的不知道这是为什么。

这里的源代码:

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

}

这里完整例外,它抛出:

运行: 异常线程 “main” java.nio.file.NoSuchFileException :D:\ Benutzerdaten \ Paul \ Documents \ NetBeansProjects \ Fotosorter \ bitmaps \ java-temp \ 2008 \ Sep \ cimg2709。D:\ Benutzerdaten \ Paul \ Documents \ NetBeansProjects \ Fotosorter \ bitmaps \ java-fotos \ cimg2709.jpg。 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) 在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秒)

正如您所猜测它尚未完成。除了解决我之前说过的问题之外,我仍然需要将它付诸实践。

+0

是否存在D:\ Benutzerdaten \ Paul \ Documents \ NetBeansProjects \ Fotosorter \ bitmaps \ java-temp \ 2008?为什么只有在月份的父文件不存在的情况下才调用mkdirs(),而如果月份本身不存在,那么为什么不调用mkdirs()? –

+0

最近的编辑表明,本网站不要求礼貌。而且,真正的问题,或者说“请求帮助和启发”被大胆地删除。我希望这不会影响我的问题得到解决的机会。 – user3026231

+0

不,它没有。但回答评论中提出的问题将有助于获得答案。 –

回答

0

确保输入文件存在。

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

相关问题