I am trying to make a directory in Java. If it exists, I want to delete that directory and its content and make a new one. I am trying to do the following, but the directory is not deleted. New files are appended to the directory.
File file = new File("path");
boolean isDirectoryCreated = file.mkdir();
if (isDirectoryCreated) {
System.out.println("successfully made");
} else {
file.delete();
file.mkdir();
System.out.println("deleted and made");
}
I am creating this directory in runtime in the directory of the running project. After every run, the old contents have to be deleted and new content has to be present in this directory.
1 Answer