Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java program that changes a file's extension

 
Sridhar Venkataraman
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to write a java program that when executed can take a file or a certain format (say .txt) and give as output the same file, but with the extension changed (say .xml).

How do I go about accomplishing this? Thank you.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The name of a file -including its extension- can be changed by the java.io.File.renameTo method.

Your example is strange, though - an .xml extension implies that the file is valid XML, which wouldn't be the case for just any old .txt file. (Changing the file extension does not change the file type or file contents.)
 
Sridhar Venkataraman
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My example was hypothetical. I actually have the extension .mm (a FreeMind mindmap file), which is valid XML all the time. So I just want to change the file extension for another program to read and parse the XML contained in the file.

Thanks!
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
split() can be your friend.
 
Sridhar Venkataraman
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will attempt to implement it and get back if I have any problems. Thank you.
 
K Sathya Narayanan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above program changes the extension of *.txt to *.xml. if ext file is not found , then it creates a file in that name and renames its extension to xml. files with other extension are not renamed or created.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That code is error prone.

First of all, what if there is no extension? indexOf will return -1, which will cause an IndexOutOfBoundsException. Secondly, what if there are multiple dots in the full filename? lastIndexOf will solve that.

And what if the filename contains the extension twice?

Your code, slightly modified:

[ May 28, 2008: Message edited by: Rob Prime ]
 
Anthony Mag
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.File;
import java.util.StringTokenizer;

/*Change the extension name of the file recursively
for multiple directories
*/
public class renFile {
static String reName ="";
static StringTokenizer str =null;
static File files[] =null;
static final String ext = ".doc";//Extension to Change to
public void Innundo(String path){
String t;
try {
File file = new File(path);
files = file.listFiles();
for(File fl : files){
if(fl.isDirectory()){
System.out.println("Directory name:"+fl.toString());
Innundo(fl.toString());

}else {
System.out.println("File name:"+fl.toString());
if(fl.isFile()){
t = fl.getName();
str = new StringTokenizer(t,".");
if(str.hasMoreTokens()){
t= str.nextToken();
t= fl.getParent()+"\\"+t+ext;
System.out.println("Changed Name "+t);
fl.renameTo(new File(t));
}
}
else {System.out.println(fl.getName()+" Not A File! ");}
}
}
}catch(Exception e){
e.printStackTrace();

}
}
public static void main(String[] args) {
String path="C:/Path_To_Directory";
renFile re = new renFile();
re.Innundo(path);
}
}
//Internet Resources
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony Gomes, don't forget the "Code" button, otherwise your code becomes illegible. Please go back and use the edit button to add code tags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic