Java Rename File
- Renaming a file is the process of changing its name. Using the renameTo() function of the Java File class, renaming operations are possible.
- A file can be renamed using Java's renameTo() function. This method is, however, quite platform-specific; you might be able to properly rename a file under *nix but not in Windows.
- Therefore, it is always advisable to check the return result to ensure that the file has been successfully renamed (true if successful, false if unsuccessful).
- The Files.move() method can be used to relocate or rename the empty directory. If the folder is not empty, then a move is permitted if it is possible to transfer the directory without moving the directory's contents.
- Recursively calling move for the files and subdirectories in a directory would be necessary to transfer the directory and all of its contents. This will be covered in a later article.
- A built-in class called java.io.File is available in Java for common file operations. This can also be used to rename files.
Rename File Example
- Make a "sample.txt" file in the "C:/workspace" directory.
- By providing the absolute file location path "C:/workspace/sample.txt," create a File class object.
- To rename the file, we must supply a special abstract pathname to the renameTo() method.
- The renameTo() method only returns true if the renaming was successful; otherwise, it returns false.
- Check the directory to see if the file has been renamed.
Program
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RenameFile {
private static final Logger LOGGER = LoggerFactory.getLogger(DeleteFileExample.class);
public static void main(String[] args) {
renameFile();
}
public static void renameFile() {
File file = new File("C:/workspace/sample.txt");
booleanhasRename = file.renameTo(new File("C:/workspace/sample2.txt"));
if (hasRename) {
LOGGER.info("File is renamed successfully");
} else {
LOGGER.info("File reanme failed");
}
}
}
There are some other approaches to renaming a file.
- ReanameTo() Method
- Move() Method
1. RenameTo() Method
A file's abstract pathname can be changed to a specific pathname using the RenameTo() method. If any file is renamed, the procedure returns true; otherwise, it returns false.
Approach
- Create a File class object and change the file path to the directory location.
- The file path should be changed to the directory's renaming path in a new object of a File type.
- RenameTo() should be used.
- Renaming successfully results in the function returning true.
- Returns false if Else.
Program
import java.io.File;
public class Demo {
public static void main(String[] args)
{
File file = new File("C:\java\Demo);
File rename = new File("C:\java\Demo1);
boolean flag = file.renameTo(rename);
if (flag == true) {
System.out.println("File Renamed Successfully ");
}
else {
System.out.println("File Reanme Failed");
}
}
}
Output
File Renamed Successfully
2. move() Method
Moving the information from the initial file to the new file as well as deleting the original file can be used to rename a file. Java's resolveSibiling function is being used to handle this action. It is used to compare the given path to the parent path of this path.
Approach
- Create a File class object and change the file path to the directory location.
- The file path should be changed to the directory's renaming path in a new object of a File type.
- RenameTo() should be used.
- Renaming successfully results in the function returning true.
- Returns false if Else.
Program
import java.nio.file.*;
import java.io.IOException;
public class Demo {
public static void main(String[] args)
throws IOException
{
Path oldFile
= Paths.get("C:\java\program.java");
try {
Files.move(oldFile, oldFile.resolveSibling(
"program1.java"));
System.out.println("File Renamed Successfully ");
}
catch (IOException e) {
System.out.println(" File Rename failed");
}
}
}
Output
File Renamed successfully
Example Programs
Example 1
import java.io.File;
public class Main{
public static void main(String[] args) {
File oldName = new File("C:/java/hello.java");
File newName = new File("C:/java/hi.java");
if(oldName.renameTo(newName)) {
System.out.println("File renamed successfully");
} else {
System.out.println("File rename failed");
}
}
}
Output
File Renamed successfully
Example 2
import java.io.File;
public class RenameFileExample {
public static void main(String[] args) {
File oldfile = new File("C:/java/hello.java"
File newfile = new File("C:/java/bye.java");
if(oldfile.renameTo(newfile)) {
System.out.println("File Renamedsuccesful");
} else {
System.out.println(" File Rename failed");
}
}
}
Output
File Renamed failed
Conclusion
In this article, we looked at various Java file-moving methods. In these code snippets, we concentrated on renaming, but moving is, of course, the same; the only difference is in the target directory.