How to rename a file in java

In this post, we will see how to rename a file in java. We can use 's rename(File dest) method to rename a file. rename method returns true if operation is success else returns false. It can use this method to move a file also.

You should always check if rename operation is successful or not because it does not throw any exception

Java Program:

package org.arpit.java2blog;


public class RenameFileMain {
  * @Author : Aprit Mandliya

 public static void main(String[] args) {

  // Read the file
  File configFile=new File("/Users/Arpit/Desktop/");
  // destination file object
  File configFileNew=new File("/Users/Arpit/Desktop/");
  boolean remameSuccess=configFile.renameTo(configFileNew);

   System.out.println("Rename operation is successful");
   System.out.println("Rename operation is unsuccessful");



When I ran above program, I got following output:
Rename operation is successful

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012