Rename a file in Java
This post will discuss how to rename a file in Java.
We can rename a file in Java in different ways, with or without the NIO package. We can also use some third-party libraries that provide utility methods for renaming or moving files or directories, such as Guava or Commons IO. These are discussed below in detail:
1. Using File.renameTo() method
Before Java 7, we can call the renameTo() method on a File object to rename it. This method takes another File object as a parameter and tries to rename the file or directory represented by the current object to the name and path of the given object. We should always check the return value of this method to see if the rename operation was successful or not. This is because behavior of this method is inherently platform-dependent, and it doesn’t even throw an IOException on failure. For example, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.io.File; import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.NoSuchFileException; class Main { public static void main(String[] args) { try { // original file File file = new File("demo.txt"); if (!file.exists()) { throw new NoSuchFileException("The source file does not exist."); } // target file File dest = new File("demo_success.txt"); if (dest.exists()) { throw new FileAlreadyExistsException("The destination path exists."); } boolean success = file.renameTo(dest); if (success) { System.out.println("File successfully renamed"); } } catch (IOException e) { e.printStackTrace(); } } } |
Note if the file is renamed successfully, a success message is displayed. If the source file doesn’t exist, NoSuchFileException is thrown, and if the destination file is already present, FileAlreadyExistsException is thrown.
2. Using Files.move() method
The Files class in Java 7 provides various static methods that work with files, directories, and other file types. To rename or move a file to a target file, we can use the move() method of the Files class from the NIO package. This method takes two Path objects as parameters and moves or renames the file or directory from the source path to the target path. We can also specify some options to control how the move operation is performed, such as replacing an existing file or copying attributes. For example, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; class Main { public static void main(String[] args) { Path source = Paths.get("demo.txt"); try { Files.move(source, source.resolveSibling("demo_success.txt")); System.out.println("File successfully renamed"); } catch (IOException e) { e.printStackTrace(); } } } |
3. Using Guava Library
Guava’s Files class has several utility methods for working with files. To rename a file using Guava, we can use the Files.move() method of the com.google.common.io.Files class. This method takes two File objects as parameters and moves the file or directory from the source to the destination. It also handles the cases where the destination is on another file system or where an existing file needs to be overwritten. For example, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import com.google.common.io.Files; import java.io.File; import java.io.IOException; class Main { public static void main(String[] args) { try { // original file File file = new File("demo.txt"); // target File dest = new File("demo_success.txt"); Files.move(file, dest); System.out.println("File successfully renamed"); } catch (IOException e) { e.printStackTrace(); } } } |
4. Using Apache Commons IO
We can also use the FileUtils.moveFile() method from the Apache Commons IO library, which is similar to Guava’s Files.move() method. This method takes two File objects as parameters and moves the file or directory from the source to the destination. It also handles the cases where the destination is on another file system or where an existing file needs to be overwritten. For example, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; class Main { public static void main(String[] args) { try { // original file File file = new File("demo.txt"); // target File dest = new File("demo_success.txt"); FileUtils.moveFile(file, dest); System.out.println("File successfully renamed"); } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about renaming a file in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)