2014-01-26 149 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

確保輸入文件存在。

但也要確保目標文件夾的路徑確實存在。

相關問題