File index = new File("/home/Work/Indexer1");
if (!index.exists())
{
index.mkdir();
System.out.println("Dir Not present. Creating new one!");
}
index.delete();
System.out.println("File deleted successfully");
File index = new File("/home/Work/Indexer1");
if (!index.exists())
{
index.mkdir();
}
else{
System.out.println(index.delete());//If you get true then file is deleted
if (!index.exists())
{
index.mkdir();// here you are creating again after deleting the file
}
}
public static void deleteFile(File element) {
if (element.isDirectory()) {
for (File sub : element.listFiles()) {
deleteFile(sub);
}
}
element.delete();
}
Path index = Paths.get("/home/Work/Indexer1");
if (!Files.exists(index)) {
index = Files.createDirectories(index);
} else {
Files.walk(index)
.sorted(Comparator.reverseOrder()) // as the file tree is traversed depth-first and that deleted dirs have to be empty
.forEach(t -> {
try {
Files.delete(t);
} catch (IOException e) {
// LOG the exception and potentially stop the processing
}
});
if (!Files.exists(index)) {
index = Files.createDirectories(index);
}
}
import org.apache.commons.io.FileUtils;
// First, remove files from into the folder
FileUtils.cleanDirectory(folder/path);
// Then, remove the folder
FileUtils.deleteDirectory(folder/path);
You can simply remove the directory which contains a single file or multiple files using the apache commons library.
梯级进口:
实现组: ‘ commons-io’,名称: ‘ commons-io’,版本: ‘2.5’
File file=new File("/Users/devil/Documents/DummyProject/hello.txt");
File parentDirLocation=new File(file.getParent);
//Confirming the file parent is a directory or not.
if(parentDirLocation.isDirectory){
//after this line the mentioned directory will deleted.
FileUtils.deleteDirectory(parentDirLocation);
}