Java file deletion fails -
i need delete files within java program , have written code. fails delete file , can't figure why. file not in use , not write protected.
public static void delfile(string filetodel) { try { file file = new file("filetodel"); if (file.delete()) { system.out.println(file.getname() + " deleted!"); } else { system.out.println("delete operation failed." + filetodel); } } catch (exception e) { e.printstacktrace(); } }
if want delete file, there no need loading it.
java.nio.file.files.deleteifexists(filetodel);
(where filetodel contains path file)
returns true if file deleted, can put in if-clause.
Comments
Post a Comment