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


 
  
 
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
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Java Change File-Extension

                         

In this program you will learn how to change the file extension of the file. Here you will be asked to enter the file name whose extension is to be changed and then you will get the file name with the changed extension..

Description of the code:

In the program code given below, you will be asked to enter the file name with the extension as can be observed from the method of java.io.*; package which is BufferedReader();. The compiler will read the file name as oldfileExtension once you enter it through readLine(); method. Then it will check for that filename if it exists or not. If it will find the mentioned filename then change it by asking you to enter a new fileextension. For this we have used a constructor and we have passed the newfileExtension to that. Then we have applied renameTo(); to change the filename. Remember we haven't changed the filename here but file extension only.

The code of the program is given below:

import java.io.*;

public class ChangeFileExt{
  public static void main(String[] argsthrows IOException{
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please enter the filename with extension to be changed: ");
    String oldfileExtension = in.readLine();
    File oldfile = new File(oldfileExtension);
    if(!oldfile.exists())
    {
      System.out.println("File does not exist.");
      System.exit(0);
    }
    int dotPos = oldfileExtension.lastIndexOf(".");
    String strExtension = oldfileExtension.substring(dotPos + 1);
    String strFilename = oldfileExtension.substring(0, dotPos);
    String newfileExtension = in.readLine();
    String strNewFileName = strFilename + "." + newfileExtension;
    File newfile = new File(strNewFileName);
    boolean Rename = oldfile.renameTo(newfile);
    if(!Rename)  {
      System.out.println("FileExtension hasn't been changed successfully.");
    }
    else {
      System.out.println("FileExtension has been changed successfully.");
    }
  }
}

Output of the program:

C:\java-examples>javac ChangeFileExt.java

C:\java-examples>java ChangeFileExt
Please enter the filename with extension to be changed: amit.txt
Enter file extension to change the file type: java
FileExtension has been changed successfully.

C:\sourcecontrol\roseindia\public_html\java\string-examples>

Download this example. 

                         

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.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
Image Reader
read/writetextfileusin
hide textbox in jsp
String to array in jav
grantchart
change color in JTabl
SAX
draw line in java
to set combobox
classsystem
flush
Blender 3d Basics Meta
jQuery to Auto Image S
logim.servlet program
read keys of propertie
Hash Function in Java
Combattons
sequence number genara
image upload
Photoshop Ghost effect
addtwonumbersinjavausi
difference between J2S
deployapplication
easy graphics
MDB EJB 2.0
JVM vs Interpretor
draw triangle
cancel button in Strut
example for polymorphi
delayu
login jsp
xml file using checkbo
sonicmq
jsf chat tomcat
j2me game
how to create date?
tokenstring
new function
displaying text in a d
jsp export to pdf
adding label and textb
howtoexecutthe.batfile
edit Employee Records
count the numbet of di
layout:datagrid
userdefinedexceptionsa
character to decimal
UPDATE QUERY JAVA
sÃ? Ã?¸Ã?¦Ã?¯Ã?¿Ã
how to highlight a par
Paint Shop Pro Web Gra
eventJTable
select a row and edit
jpaspringtransaction
spring Tutorials
ecosurePortalUnsavedDa
json lib/componen...]=
makekeyboardcharactere
create txt file in spe
getting data from data
btreeimplementation
Web Site promotion ser
retrieve image from my
richfaces tutorial
spring
Jmu\enu in java
create two rectangle o
Secure Your Sockets wi
connectionpoolingsqlse
msql query to get high
util.collection operat
java quiz
max length 500 chars
Sql server through jsp
richfaces
drill
selectManyCheckbox
menubar in jsp
equals
implementing stack usi
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 | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.