Search code examples
javanio

How to copy multiple files with different extensions using Java 8?


I have different kind of files in my working directory (.log, .xml,.opf so on). I need to copy them for another folder.But only one file is copied as I understand this is because of using StandardCopyOption.REPLACE_EXISTINGin copy method. Here is my Java code

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

Please help me to solve this issue. Thanks in advance..!


Solution

  • Files.copy second parameter is not directory, but file name.

    It should be:

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