Java Files move a directory

Introduction

When a directory is moved on the same file store, then the directory and subdirectories are moved.

The following will move the docs directory, its files, and its subdirectories to the music directory as follows:

Path sourceFile = Paths.get("C:/home/docs"); 
Path destinationFile = Paths.get("C:/home/music/docs"); 
Files.move(sourceFile, destinationFile); 

Moving a directory across file stores will result in an exception if the directory is not empty.

Path sourceFile = Paths.get("C:/home/docs"); 
Path destinationFile = Paths.get("E:/home/music/docs"); 
Files.move(sourceFile, destinationFile); 

To move a non-empty directory across file stores, then this will normally involve a copy operation followed by a delete operation.




PreviousNext

Related