Reputation: 386
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.
Upvotes: 4
Views: 6900
Reputation: 124
I had this problem on a zipFileSystem but discovered I needed to call fileSystem.close() before it actually got removed from the zip.
Upvotes: 0
Reputation: 11841
I was encountering this problem with Apache Lucene when trying to commit an IndexWriter
. The problem was an IndexSearcher
opened on the directory, resulting in a java.nio.file.atomicmovenotsupportedexception
. The IndexReader
constructor argument was provided by DirectoryReader.open(Directory directory)
. Changing to use the overload DirectoryReader.open(IndexWriter writer)
fixed the problem.
Upvotes: 0
Reputation: 386
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.
Upvotes: 2