Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (post your own) View All Comments Latest 10 Comments:

I am embedded software developer for ASML in Wilton, Connecticut. This example was very helpful.

Ed Chen
Connecticut, USA

Posted by Ed Chen on Saturday, 03.1.08 @ 21:51pm | #50877

Hi
This topics having some error so you please help how it will work

Posted by palani on Wednesday, 08.29.07 @ 22:16pm | #24444

The program that you have specified is not properly executed under windows, i have tried most of the program like you you have mention all are not able to rename the file ...... can you specify any alternative to rename the file

Posted by Rahul Kandurwar on Tuesday, 05.8.07 @ 14:45pm | #15450

very nice solution to my problem at this moment of hurry

Posted by preeti on Monday, 11.27.06 @ 22:09pm | #157

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.