This program shows how to rename a file. It accepts two arguments, the first of which is the name of the file to rename and the second
of which is the new name of the file.
===================================================================
public class Rename {
public static void main(String[] args) {
File source, destination;
if(args.length != 2) {
System.err.println("Usage: Rename ");
return;
}
source = new File(args[0]);
if(!source.exists()) {
System.err.println(source + " doesn't exist!");
return;
}
destination = new File(args[1]);
if(destination.exists()) {
System.err.println(destination + " already exists!");
return;
}
try {
if(!source.renameTo(destination))
System.err.println("Failed.");
} catch(SecurityException e) {
System.err.println("Permission denied.");
return;
}
}
}