Search code examples
javanio

java.nio.file Files.move() is copying instead of moving


I'm trying to use Java NIO Files.move method to move a directory. It does copy the directory contents to the new location, but it leaves the old directory in place. I would consider this a copy operation and not a move operation.

Any ideas why this is happening? Here is my code:

Path source = FileSystems.getDefault().getPath("C:\\test-source");
Path destination = FileSystems.getDefault().getPath("C:\\test-destination");
try {
    System.out.println("Moving files ...");
    Files.move(source, destination, StandardCopyOption.ATOMIC_MOVE);
    System.out.println("Done.");
} catch (IOException e) {
    System.out.println("Moving failed: " + e.toString());
}

Again, the destination directory appears with all its contents, but the source folder remains in place.


Solution

  • It turns out that the code is correct. But the source folder is not being deleted because another process is still working with that folder. When I eliminate the other process (an AWS S3 directory download to the source folder), the move happens as I would expect.