Home Java Example Java Io Rename the File or Directory in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Rename the File or Directory in Java

Advertisement
In this section, you will see that how a file or directory is renamed. This program illustrates you the procedure of doing so. Through this program you can easily rename any type of the file or directory.

Rename the File or Directory in Java

     

Introduction

In this section, you will see that how a file or directory is renamed. This program illustrates you the procedure of doing so. Through this program you can easily rename any type of the file or directory. If you mention the directory (path) with the new file or directory name to rename the old file or directory which has to be renamed then the file or directory will be moved from it's own directory to the specified new directory otherwise file or directory will be moved from it's own directory to the default directory where your program has been running. This program also tells you about the availability of the specified file or directory which has to be renamed.

The method renameTo(new_file_instance) can be used to rename the appropriate file or directory. Method renameTo() renames the specified file or directory and returns a boolean value (true or false). Syntax of the renameTo() method used in this program : 

oldfile.renameTo(newfile)

oldfile : oldfile is the created instance of the File class for holding the specified file or directory name which has to be renamed in the file format.
newfile : newfile is also the instance of File class, which is the new name of file or directory

There are no any typical logic has been used in this program. Simply this program first reads the file or directory name which has to be renamed and then check whether the specified file or directory exists or not. If the specified file or directory exists then the program renames that file or directory into the name specified as parameter to the renameTo() method.

Here is the code of the program : 

import java.io.*;

  public class RenameFileOrDir{
 
 
public static void main(String[] argsthrows IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Please enter the file or directory 
   name which has to be Renamed : "
);
  
 
String old_name = in.readLine();
  
File oldfile = new File(old_name);
 
 
 
if(!oldfile.exists())
{
 
  
System.out.println("File or directory does not exist.");
  
  
System.exit(0);

  }
 
 
System.out.print("please enter the new file or directory name : ");
 
  
String new_name = in.readLine();

  File newfile = new File(new_name);
 
  
System.out.println("Old File or directory name : "+ oldfile);
  System.out.println("New File or directory name : "+ newfile);
  boolean Rename = oldfile.renameTo(newfile);
  

    if(!Rename)
{

  System.out.println("File or directory does not rename successfully.");

    System.exit(0);
  
  
}
 
 
else {

  System.out.println("File or directory rename is successfully.");

  }

  }

}

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 22, 2008

Ask Questions?    Discuss: Rename the File or Directory in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Anand
April 18, 2011
How to rename file and folder ?

I Question is how we can change the file and folder name without changing he source file and folder name.
Anand
April 18, 2011
Hi

I Question is how we can change the file and folder name without changing he source file and folder name. for example I have file a.txt and folder A. Now I want to create a copy of file and folder with diffrente name that would be change the name by b.txt and B . so without changing the source file and folder name (i.e. a.txt and A) how i CAN CREATE THE COPY OF THE SAME AT MY NEW LOCATION ?
Phan
November 17, 2011
this code can run in JSP ???

Sorry, this code has running in Java. But I want its can running in JSP ??? Thanks for shared ^-^
Elizabeth
April 18, 2012
Rename code

Thank you. I was able to modify my class assignment by following the directions of your posting. I made sure I referenced your material.
DMCA.com