Categories
functions java

JAVA:Renaming Files

To rename a file in java you first declare the file variable with oldname and next one with newname and
file1.rename2(file2);
[java]
public static void renamedirectory(String oldname,String newname){
// File (or directory) with old name

File file = new File(oldname);

// File (or directory) with new name
File file2 = new File(newname);

// Rename file (or directory)
boolean success =false;
if(file.isDirectory()){
success = file.renameTo(file2);
} else {
System.out.println(“The file is not a directory “+oldname) ;
}
if (!success) {
System.out.println(“File was not successfully renamed”);
} else {
System.out.println(“File Renamed from “+oldname+” to “+newname);
}
}
[/java]
The above function prints out if it doesnot rename succesfully.