To rename or move a file/directory in Java, you can use either the renameTo() method of a File  object in the old File I/O API, or the Files.move() method in the new Java NIO API.

 

1. Rename File/Directory Example with renameTo() method

You can use the renameTo() method to rename a file or directory to a new name which does not exist.

The following example renames a file to a new name in the current directory:

File sourceFile = new File("Notes.txt");
File destFile = new File("Keynotes.txt");

if (sourceFile.renameTo(destFile)) {
	System.out.println("File renamed successfully");
} else {
	System.out.println("Failed to rename file");
}
As you can see in this example, the renameTo() method returns a boolean value indicating the renaming succeeded (true) or failed (false) - so you should always check its return value.

If the destination file exists, the method returns false.

The following example renames the directory “test” to “dist” in the current directory:

File sourceFile = new File("test");
File destFile = new File("dist");

if (sourceFile.renameTo(destFile)) {
	System.out.println("Directory renamed successfully");
} else {
	System.out.println("Failed to rename directory");
}
If the destination directory exists, the method return false.

 

2. Move File Example with renameTo() method



If the path of the destination File points to another directory, the source file will be moved. The following example moves a file from the current directory to another one (also renames it):

File sourceFile = new File("CoverPhoto.png");
File destFile = new File("D:/Photo/ProfilePhoto.png");

if (sourceFile.renameTo(destFile)) {
	System.out.println("File moved successfully");
} else {
	System.out.println("Failed to move file");
}
NOTE: You cannot use the renameTo() method to move directory, even the directory is empty.

This method is platform-dependent and is not guaranteed to work in all cases. So it is recommended to use the Files.move() method in Java NIO as described below.

 

3. Rename File/Directory Example with Files.move() method

The static move() method of the Files class in the java.nio.file package is platform-independent and have options to replace the destination file if exists:

     Files.move(Path source, Path target, CopyOptions… options)

This method returns the path to the target file, and throws exception if the operation failed.

The following example renames a file in the current directory:

Path source = Paths.get("Notes.txt");
Files.move(source, source.resolveSibling("Keynotes.txt"));
If the target file exists, this method throws java.nio.file.FileAlreadyExistsException. You can specify the option to replace the existing file like this:

Path source = Paths.get("Notes.txt");
Files.move(source, source.resolveSibling("Keynotes.txt"),
			StandardCopyOption.REPLACE_EXISTING);
The following example renames a directory to a new one:

Path source = Paths.get("photo");
Files.move(source, source.resolveSibling("photos"));
If the target dir exists, it throws FileAlreadyExistsException. You can fore to replace the existing directory with the copy option:

Path source = Paths.get("photo");
Files.move(source, source.resolveSibling("photos"),
				StandardCopyOption.REPLACE_EXISTING);
However, this works only if the target directory is not empty. Otherwise, it throws java.nio.file.DirectoryNotEmptyException.

 

4. Move File/Directory Example with Files.move() method

The following example illustrates how to move a file from the current directory to another (keeping the same file name) and replace the existing file:

Path source = Paths.get("CoverPhoto.png");
Path newdir = Paths.get("D:/Photo");
Files.move(source, newdir.resolve(source.getFileName()), 
			StandardCopyOption.REPLACE_EXISTING);
And you can move an empty directory to another location, for example:

Path source = Paths.get("test");
Path newdir = Paths.get("D:/Photo");
Files.move(source, newdir.resolve(source.getFileName()));
NOTE: You can move only empty directory and replace existing directory if it is also empty. In either a directory is not empty, a DirectoryNotEmptyException is thrown.

 

API References:

 

Related File IO Tutorials:

 

Other Java File IO Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#2john faro2020-06-30 07:59
Great tutorial!! Thank for for this information.
Quote
#1James2020-03-14 07:45
Sir I want to replace (C:\Users\Aldren\Desktop\5.txt) 5.txt with (new)5.txt

exactly C:\Users\Aldren\Desktop\(new)5.txt
Quote