How to rename file in java

Let’s learn how to rename file in java.

how to rename file in java

Renaming file in java

To rename a file, java file renameto method of File class is used. Java file renameto method returns true if and only if the renaming succeeded; false otherwise.

Here in the below java example first we are predefining pathname as parameter in File class.

// Renames the file denoted by this abstract pathname

public boolean renameTo(File dest)

Many aspects of the behavior of renameTo() method are inherently platform-dependent. Here’s the rename file in java example,

import java.io.*;

public class RenameFile 
{
   public static void main(String[] args) throws IOException 
   {
      File oldFile = new File("D:/Project/flower.txt");
      File newFile = new File("D:\\Project\\flowerbrackets.txt");
      if(oldFile.renameTo(newFile))
      {
         System.out.println("Rename successful");
      }
      else
      {
         System.out.println("Rename failed");
      } 
   }
}



Output:

Rename successful


Also read – random number generator in java

java 8

In java 8 rename file there is a class java.nio.file.Files for renaming files. Let’s learn how to rename java file in eclipse,

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

public class RenameFileDemo 
{
   public static void main(String[] args) 
   {
      Path oldPath = Paths.get("D:/Project/flower.txt");
      Path newPath = Paths.get("D:/Project/flowerbrackets.txt");
      try 
      {
         Files.move(oldPath, newPath, StandardCopyOption.REPLACE_EXISTING);
         System.out.println("Successfully renamed");
      } 
      catch(IOException ioe) 
      {
         ioe.printStackTrace();
         System.out.println("Unable to rename file");
      }
   }
}



Output:

Successfully renamed

code files a folder

Here we are renaming files in a folder. Often it is very easy to rename two or more files in a folder.

But what if we have 100 to 200 files to rename? So let’s learn java code to rename files in a folder.

import java.io.File;

public class RenameAllFilesDemo 
{
   public static void main(String[] args) 
   {
      // folder path 
      String strPath = "D:\Users\sachin\java\sachinfolder";
      // let's create new folder
      File newfolder = new File(strPath);
      File[] arrFile = newfolder.listFiles();
      for(int a = 0; a < arrFile.length; a++) 
      {
         if(arrFile[a].isFile()) 
         {
            File file = new File(strPath + "\" + arrFile[a].getName()); 
            String strFileName = arrFile[a].getName(); 
            String[] tokens = strFileName.split("\s"); 
            String strNewFile = tokens[1]; 
            System.out.println(strFileName); 
            System.out.print(strNewFile);
            file.renameTo(new File(strPath + "\" + strNewFile + ".pdf")); 
         } 
      }
   }
}



Extension

Now let’s learn java rename file extension. Here’s the java program,

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RenameFileExtension 
{
   public static boolean renameExtension(String strSource, String strNewExtension)
   {
      String target;
      String strCurrent = findExtension(strSource);
      if(strCurrent.equals(""))
      {
         target = strSource + "." + strNewExtension;
      }
      else 
      {
         target = strSource.replaceFirst(Pattern.quote("." + strCurrent) + "$", Matcher.quoteReplacement("." + strNewExtension));
      }
      return new File(strSource).renameTo(new File(target));
   }

   public static String findExtension(String strFile) 
   {
      String strExtension = "";
      int a = strFile.lastIndexOf('.');
      if(a > 0 &&  a < strFile.length() - 1) 
      {
         strExtension = strFile.substring(a + 1);
      }
      return strExtension;
   }

   public static void main(String[] args) throws IOException 
   {
      System.out.println(RenameFileExtension.renameExtension("A://java.txt", "pdf"));
   }
}



Output:

true