![](/img/trans.png)
[英]Files.copy() in Java throws java.nio.file.NoSuchFileException
[英]Files.copy throws java.nio.file.NoSuchFileException even though the file to be copied definitely exists
我有一個看似簡單的應用程序的問題。 它應該做什么:
- 讀出(硬編碼)目錄的文件(*.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-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 秒)
正如您可能已經猜到的那樣,它還沒有完成。 除了解決我之前提到的問題之外,我還必須將其放入方法中。
確保輸入文件存在。
但也要確保目標文件夾的路徑確實存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.