Last Updated on 30 July 2019   |   Print Email
To rename or move a file/directory in Java, you can use either the renameTo() method of a Fileobject 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:
If the target file exists, this method throws java.nio.file.FileAlreadyExistsException. You can specify the option to replace the existing file like this:
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.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments
exactly C:\Users\Aldren\Desktop\(new)5.txt