File.delete () to delete a file, it will return a boolean to indicate the status of the delete operation; true if the file is deleted; false if failed.
file.renameTo (file2) to rename the file, it will return a boolean to indicate the status of the rename operation; true if the file is renamed; false if failed.
package com.software.file;
import java.io.File;
public class RenameAndDeleteFileExample
{
public static void main(String[] args)
{
try{
File file = new File("c:\\test.log");
File file2 = new File("newname");
boolean success = file.renameTo(file2);
if(file2.delete() && success ){
System.out.println(file2.getName() + " is renamed and deleted!");
}else{
System.out.println("operation is failed.");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
source
share