This function is to delete files in java. It first checks whether it exists or not next check for write access rights or permissions and then check if the directory is empty if the given file is a directory.
public static void delfile(String fname){
File f = new File(fname);
//Check whether file exists or not
if (!f.exists()){
System.out.print("file doesnot exists");
} else if (!f.canWrite()){
System.out.print("write prtected");
} else if (f.isDirectory()) {
String[] files = f.list();
if (files.length > 0){
System.out.print("this is a directory");
}
}
boolean success = f.delete();
}