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.

1. Using File.renameTo()

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

2. New IO’s Files.move()

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);

3. Guava’s Files.move()

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);

4. Common IO’s FileUtils

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);

5. Conclusion

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.

Happy Learning !!

Source Code on Github

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.