Java rename file

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

java rename file

Here in the below example first we are predefining pathname as parameter in File class. In the next step renameTo() method of File class is used to rename appropriate file.

public boolean renameTo(File dest) // Renames the file denoted by this abstract pathname

Many aspects of the behavior of renameTo() method are inherently platform-dependent. Here’s the code implementation,

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 – java example to print all alphabets

Also read – file last modified date java program

Related Posts