Java Rename File

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn java rename file.

java rename file

Let’s see java rename files in directory using java renameto() method. Example below illustrates the procedure to rename a file in java or rename any type of file or directory.

I will be showing different methods to rename a file in java,

Java rename file : using renameto method

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 RenameFile
{
   public static void main(String[] args)
   {
      File olderName = new File("C:\\FileIO\\flower.txt");
      File newerName = new File("C:\\FileIO\\flowerbrackets.txt");
 
      boolean renamedFile = olderName.renameTo(newerName);
      System.out.println("Java rename files in directory : ");
 
      if(renamedFile)
         System.out.println("File renamed!!");
      else
         System.out.println("ERROR - Try once again!!");
   }
}

Output:

Java rename files in directory :

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.


Rename ‘n’ number of files

Here in this method let us understand renaming files in java or how to rename ‘n’ number of files or directory in java.

At first, we are defining a function callRename() consisting of three string parameters namely String flname – name of the file or directory to rename.

Also Read – Java Example To Print All Alphabets

String fromName – renaming file or directory from and String toName – renamed file or directory name.

In the next step for loop loops around to rename file or directory.

Here’s example,

import java.io.File;

public class RenameFile
{ 
   public String callRename(String flname, String fromName, String toName)
   {
      try
      {
         File fe = new File(flname);
         File allFile[] = fe.listFiles();
         for(int a = 0; a < allFile.length; a++)
         {
            String presentName = (allFile[a].toString().replaceAll(fromName, toName));
            allFile[a].renameTo(new File(presentName));
         }
         return allFile + " files renamed successfully.!!!";
      }
      catch(Exception ae)
      {
         return(ae.getMessage());
      }
   }

   public static void main(String[] args)
   {
      RenameFile rf = new RenameFile();
      System.out.println("Java rename files in directory : ");
      String lastResult = rf.callRename("C:\\FileIO\\", "in", "to");
      System.out.println(lastResult);
   }
}

Output:

Java rename files in directory :

Before renaming file or directory

java rename file

After renaming file or directory

java rename file


Using BufferedReader class

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.

Also Read – File Last Modified Date Java Program

In the next step, the user entered file or directory name is stored in pastName 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 RenameFile
{
   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 pastName = br.readLine();
      File fl = new File(pastName);
 
      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 presentName = br.readLine();
      File nuFile = new File(presentName);
      boolean renameFile = fl.renameTo(nuFile);
 
      if(!renameFile)
      {
         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: " + presentName);
      } 
   }
}

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


How to move files in java

Now let’s learn how to move files in java or how to move file from one directory to another,

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FilesMoveJava 
{
   public static void main(String[] args) throws IOException
   { 
      Path strPath = Files.move(Paths.get("A:\\java.txt"), Paths.get("A:\\Root\\javaExample.txt"));
 
      if(strPath != null)
      {
         System.out.println("File moved successfully!!!");
      }
      else
      {
         System.out.println("Failed to move!!!");
      }
   }
}

Output:

File moved successfully!!!


How to delete files in java

Let’s see how to delete files in java. Here we are copying the file and deleting the original file.

import java.io.File;

public class FilesDeleteJava 
{
   public static void main(String[] args) 
   {
      File strFile = new File("A:\\java.txt"); 
      if(strFile.renameTo(new File("A:\\Root\\javaExample.txt")))
      {
         strFile.delete();
         System.out.println("File deleted successfully!!!");
      }
      else
      {
         System.out.println("Failed to move!!!");
      }
   }
}

Output:

File deleted successfully!!!


conclusion

That’s it guys. This is all about java rename file. I hope you have understood the concept of java rename directory.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Related Posts