如何使用Java 8复制具有不同扩展名的多个文件?



我的工作目录中有不同类型的文件(.log,.xml,.opf等(。我需要将它们复制到另一个文件夹。但是只有一个文件被复制,因为据我了解,这是因为在复制方法中使用了StandardCopyOption.REPLACE_EXISTING。 这是我的Java代码

String currentDirectory = new File(new File("").getAbsolutePath()).getPath();
tempDirPath = Files.createDirectories(Paths.get(jobFolder).resolve("output"));
try {
Files.copy(Paths.get(currentDirectory +File.separator+"content.xml"), tempDirPath, StandardCopyOption.REPLACE_EXISTING);
Files.copy(Paths.get(currentDirectory +File.separator+"content.smil"), tempDirPath, StandardCopyOption.REPLACE_EXISTING);
Files.copy(Paths.get(currentDirectory +File.separator+"content.opf"), tempDirPath, StandardCopyOption.REPLACE_EXISTING);
Files.copy(Paths.get(currentDirectory +File.separator+"content.ncx"), tempDirPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

请帮我解决这个问题。 提前感谢..!

Files.copy第二个参数不是目录,而是文件名。

它应该是:

Files.copy(Paths.get(currentDirectory +File.separator+"content.ncx"), tempDirPath.resolve("content.ncx"), StandardCopyOption.REPLACE_EXISTING);

最新更新