Best Way To Rename A File Or Directory In Java

In this post let us understand the best way to rename a file or directory in java.

Example below illustrates the procedure to rename any type of file or directory. I will be showing different methods to rename a file in java,

Method 1: Here in the below example we are predefining the pathname string of File class. In the next step renameTo() method of File class is used to rename appropriate file or directory. Then the value is returned as a boolean value. Here’s the code implementation,

import java.io.*;

public class Rename_File {

public static void main(String[] args) {
 
File older_Name = new File("C:\\FileIO\\flower.txt");
File newer_Name = new File("C:\\FileIO\\flowerbrackets.txt");
 
boolean renamed_File = older_Name.renameTo(newer_Name);
System.out.println("Best Way To Rename A File Or Directory In Java");
 
if(renamed_File)
System.out.println("File renamed!!");
else
System.out.println("ERROR - Try once again!!");
}
}

Output:

Best Way To Rename A File Or Directory In Java

File renamed!!

Explanation: As you can see in the above example, program reads the file or directory name for which it has to be renamed and then it checks whether the given file or directory exists or not. In the next step, if the given file or directory exists then the program renames file or directory into the name specified as parameter to the renameTo() method.


Method 2: Here in this method let us understand how to rename ‘n’ number of files or directory in java. At first, we are defining a function call_Rename() consisting of three string parameters namely String fl_name – name of the file or directory to rename, String from_Name – renaming file or directory from and String to_Name – renamed file or directory name. In the next step for loop loops around to rename file or directory. Here’s example,

Also Read – Java Example To Print All Alphabets

import java.io.File;

public class Rename_File {
 
public String call_Rename(String fl_name, String from_Name, String to_Name)
{
 
try
{
File fe = new File(fl_name);
File all_File[] = fe.listFiles();
for(int a = 0;a < all_File.length;a++)
{
String present_Name = (all_File[a].toString().replaceAll(from_Name, to_Name));
all_File[a].renameTo(new File(present_Name));
}
return all_File + " files renamed successfully.!!!";
}
catch(Exception ae)
{
return(ae.getMessage());
}
}

public static void main(String[] args) {
 
Rename_File rf = new Rename_File();
System.out.println("Best Way To Rename A File Or Directory In Java");
String last_Result = rf.call_Rename("C:\\FileIO\\", "in", "to");
System.out.println(last_Result);
 
}
}

Output:

Best Way To Rename A File Or Directory In Java

Before renaming file or directory

Best Way To Rename A File Or Directory In Java

After renaming file or directory

Best Way To Rename A File Or Directory In Java


Method 3: Here in this method we are requesting user to enter file or directory name to rename, using BufferedReader class. This class reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays and lines. In the next step, the user entered file or directory name is stored in past_Name string variable and this string value is used as pathname string of File class. Then, the user entered directory name is checked for existence, that is, if the directory name does not exist then it will shown an error message. Finally, the directory name is renamed using renameTo() method. Now, let’s see the code implementation,

import java.io.*;

public class Rename_File {

public static void main(String[] args) throws IOException {
 
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the file or directory name which has to be renamed : ");
String past_Name = br.readLine();
File fl = new File(past_Name);
 
if(!fl.exists())
{
System.out.println("Oooops!!!!File or directory does not exist!");
System.exit(0);
}
 
System.out.println("Re-enter new file or directory name : ");
String present_Name = br.readLine();
File nu_File = new File(present_Name);
boolean rename_File = fl.renameTo(nu_File);
 
if(!rename_File)
{
System.out.println("Sorry!!!File or directory has not renamed!");
System.exit(0);
}
else
{
System.out.println("Yeaaaah!!!!File or directory renamed successfully!");
System.out.println("Details:\n");
System.out.println("Old file or directory name: " + fl);
System.out.println("New file or directory name: " + present_Name);
} 
}
}

Output:

Please enter the file or directory name which has to be renamed :
C:\FileIO\delhi_in_kolkata
Re-enter new file or directory name :
C:\FileIO\delhi_to_kolkata.txt
Yeaaaah!!!!File or directory renamed successfully!
Details:

Old file or directory name: C:\FileIO\delhi_in_kolkata
New file or directory name: C:\FileIO\delhi_to_kolkata.txt

About the author

admin

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *