The Artima Developer Community
Sponsored Link

Java Answers Forum
java works with files

2 replies on 1 page. Most recent reply: Apr 15, 2003 12:07 AM by mike wu

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
mike wu

Posts: 2
Nickname: jianj
Registered: Apr, 2003

java works with files Posted: Apr 13, 2003 3:01 AM
Reply to this message Reply
Advertisement
Can anyone tell me how to do this?

Write a small java program which can change all of the files named *.ppp to be *.xxx under a certain directory.


Dhrubo

Posts: 32
Nickname: dhrubo
Registered: Mar, 2003

Re: java works with files Posted: Apr 13, 2003 9:02 PM
Reply to this message Reply
This listing may help ya ... i had written it couple of months back

import java.io.*;
class ChangeFileNameExtension
{
public static void main(String args[])
{
String dir = "";
String newextn = "";
if(args.length == 2)
{
dir = args[0];
newextn = args[1];
}
else
{
System.out.println("Usage : java ChangeFileNameExtension <AbsoluteDirectoryPath> <NewExtenstion>");
System.exit(1);
}

File folder = new File(dir);
if(!folder.isDirectory())
{
System.out.println("Error : "+dir+" is not a directory.");
System.out.println("Program will terminate.");
System.exit(1);
}

File listOfFiles[] = folder.listFiles();

int noOfFilesCount = listOfFiles.length;
String tempFileName = "";
int index = 0 ;
for(int i = 0 ; i < noOfFilesCount ; i ++)
{

if(listOfFiles.isDirectory())
{
System.out.println("Warning : "+listOfFiles+" is not a file.");
}
else
{
tempFileName = listOfFiles.toString();
System.out.println("File : " +tempFileName);
index = tempFileName.indexOf('.');
tempFileName = tempFileName.substring(0,index+1);
System.out.println("File : " +tempFileName);
tempFileName = tempFileName + newextn;
System.out.println("Changed File Name : " +tempFileName);
listOfFiles.renameTo(new File(tempFileName));

}

}

}





}

mike wu

Posts: 2
Nickname: jianj
Registered: Apr, 2003

Re: java works with files Posted: Apr 15, 2003 12:07 AM
Reply to this message Reply
Thank your very much!

Flat View: This topic has 2 replies on 1 page
Topic: Refactoring Java Code Previous Topic   Next Topic Topic: determining mulitples

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use