To delete a file (i.e., remove it completely from the mass-storage device), we invoke the method delete() on an object of type File created with the name of the file to delete.
File f1 = new File("garbage.txt"); boolean b = f1.delete(); // if b is true, then the file has been deleted successfully
Note: The constructor of the class File does not generate an exception if the file does not exist. The result of the deletion operation is returned by the delete() method.
To rename a file, we invoke the method renameTo() on two objects of type File that represent respectively the file to rename, and the new name to give to the file.
File f1 = new File("oldname.txt"); File f2 = new File("newname.txt"); boolean b = f1.renameTo(f2); // if b is true, then the file has been renamed successfully
The file oldname.txt, if it exists, is renamed to the file newname.txt. If there already exists a file named newname.txt, then it is overwritten. The result of the renaming operation is returned by the renameTo() method.