Rename or Move a File or Directory in Java
Learn to rename a file or directory at a specified path or move to a new directory in Java. We will learn to use the classes from Standard IO, New IO, Guava and Commons IO.
As the method name suggests, renameTo() renames the file to the new name or moves the file to a new directory location.
- The renameTo() returns true or false denoting if the renaming succeeded or not.
- It throws SecurityException if there are write access problems with the old or the new file.
File originalFile = new File("c:/temp/demo.txt"); File renamedFile = new File("c:/temp/demoNew.txt"); File movedFile = new File("c:/temp/moved/demoNew.txt"); boolean isCopied = originalFile.renameTo(renamedFile); boolean isMoved = renamedFile.renameTo(movedFile); System.out.println(isCopied); //true System.out.println(isMoved); //true
The Files.move() is similar to renameTo() except it works with the Path instances instead of File instances.
- The move() method moves or renames a file to a target file. Moving a file will copy the last-modified-time to the target file if supported
- If given file and target files are same then this method has no effect.
- If target file already exists then move() will fail. We can use StandardCopyOption.REPLACE_EXISTING in such cases.
- To perform the whole rename or move operation as single atomic operation, we can use StandardCopyOption.ATOMIC_MOVE option. If the move cannot be performed as an atomic file system operation (incase of two different filesystems) then AtomicMoveNotSupportedException is thrown.
- If the file is a symbolic link then the symbolic link itself, not the target of the link, is moved.
- Renaming a directory can fail if it requires to move the files in a new location i.e. directory is being moved to a location. It it is a simple directory renaming in same location in the filesystem, it will succeed.
Path file = Path.of("c:/temp/demo.txt"); //Rename in same directory Files.move(file, file.resolveSibling("demoNew.txt")); //Move to new directory Path newDir = Path.of("c:/temp/moved/"); Files.move(file, newDir.resolve(file.getFileName()), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
This Files.move() method moves a file from one path to another. This is applicable to renaming and moving, both operations.
We should be careful that the destination path must be the target path for the file itself; not just the new name for the file or the path to the new parent directory.
File originalFile = new File("c:/temp/demo.txt"); File renamedFile = new File("c:/temp/demoNew.txt"); com.google.common.io.Files.move(originalFile, renamedFile); com.google.common.io.Files.move(renamedFile, movedFile);
The FileUtils class provides many methods to move or rename the files and directories as per requirements.
- moveDirectory(File srcDir, File destDir) – moves a directory to destination and deletes the source directory.
- moveDirectoryToDirectory(File src, File destDir, boolean createDestDir) : Moves a directory to another directory and takes an option to create the new directory or not. If createDestDir is false and new directory cannot be created then IOException will be thrown.
- moveFile(File srcFile, File destFile, CopyOption… copyOptions) : moves a file preserving file attributes with optional copy options.
- moveFileToDirectory(File srcFile, File destDir, boolean createDestDir) : moves a file into a specified directory.
- moveToDirectory(File src, File destDir, boolean createDestDir) : moves a file or directory to the destination directory.
FileUtils.moveFile(originalFile, renamedFile); File targetDirectory = new File("c:/temp/moved/"); FileUtils.moveFileToDirectory(originalFile, targetDirectory, true);
In this short tutorial, we learned to rename a file or directory or move it to a new location with different copy options.
Though standard IO and new IO classes provide all kinds of capabilities, Common IO’s FileUtils class provides very specific methods for each kind of operation. These specific method names communicate the intent very well.
Moving a File or Directory
You can move a file or directory by using the move(Path, Path, CopyOption. ) method. The move fails if the target file exists, unless the REPLACE_EXISTING option is specified.
Empty directories can be moved. If the directory is not empty, the move is allowed when the directory can be moved without moving the contents of that directory. On UNIX systems, moving a directory within the same partition generally consists of renaming the directory. In that situation, this method works even when the directory contains files.
This method takes a varargs argument – the following StandardCopyOption enums are supported:
- REPLACE_EXISTING – Performs the move even when the target file already exists. If the target is a symbolic link, the symbolic link is replaced but what it points to is not affected.
- ATOMIC_MOVE – Performs the move as an atomic file operation. If the file system does not support an atomic move, an exception is thrown. With an ATOMIC_MOVE you can move a file into a directory and be guaranteed that any process watching the directory accesses a complete file.
The following shows how to use the move method:
import static java.nio.file.StandardCopyOption.*; . Files.move(source, target, REPLACE_EXISTING);
Though you can implement the move method on a single directory as shown, the method is most often used with the file tree recursion mechanism. For more information, see Walking the File Tree.
Previous page: Copying a File or Directory
Next page: Managing Metadata (File and File Store Attributes)