How to rename/move file or directory in Java
- Details
- Written by Nam Ha Minh
- 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 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:
- How to list files and directories in a directory in Java
- Calculate total files, sub directories and size of a directory
- How to copy a directory programmatically in Java
Other Java File IO Tutorials:
- How to Read and Write Text File in Java
- How to Read and Write Binary Files in Java
- Java IO - Common File and Directory Operations Examples
- Java Serialization Basic Example
- Understanding Java Externalization with Examples
- How to execute Operating System Commands in Java
- 3 ways for reading user's input from console in Java
- File change notification example with Watch Service API
- Java Scanner Tutorial and Code Examples
- How to compress files in ZIP format in Java
- How to extract ZIP file in Java
Comments
exactly C:\Users\Aldren\Desktop\(new)5.txt