Tuesday, 10 May 2011

Rename the File or Directory in java

This example shows function, which renames the file or directory. The function takes old name and new name, and renames accordingly.

public boolean renameFile(String oldName, String newName)
{
File oldfile = new File(oldName);
if(!oldfile.exists())
{
System.out.println("File or directory does not exist.");
return false;
}
File newfile = new File(newName);
boolean reName = oldfile.renameTo(newfile);
if(!reName )
{
System.out.println("File or directory does not rename successfully.");
return reName ;
}else{
System.out.println("File or directory rename is successfully.");
return reName;
}
}

No comments:

Post a Comment