I want to create and delete a directory using Java, but it isn't working.
File index = new File("/home/Work/Indexer1");
if (!index.exists()) {
index.mkdir();
} else {
index.delete();
if (!index.exists()) {
index.mkdir();
}
}
I want to create and delete a directory using Java, but it isn't working.
File index = new File("/home/Work/Indexer1");
if (!index.exists()) {
index.mkdir();
} else {
index.delete();
if (!index.exists()) {
index.mkdir();
}
}
You can make recursive call if sub directories exists
we can use the
spring-core
dependency;Just a one-liner.
Documentation here
Guava 21+ to the rescue. Use only if there are no symlinks pointing out of the directory to delete.
(This question is well-indexed by Google, so other people usig Guava might be happy to find this answer, even if it is redundant with other answers elsewhere.)
My basic recursive version, working with older versions of JDK:
I prefer this solution on java 8:
From this site: http://www.baeldung.com/java-delete-directory