Move file in Java

To migrate any file in Java, in Java 6 or older Java versions, we can use the renameTo() method located in the File object to do this:

Result:

From Java 7 onwards, we have another way to move a file, which is to use the Files object. This Files object contains all the methods that make it possible to perform all file-related operations such as creating files, deleting files, copying files or moving files, etc. Below is the code to move a file with Files object:

And like copying files, we can even use the Apache Commons IO library to move a file.



Add Comment