how to delete folders in java code example
Example 1: How to delete a directory in java
import java.io.File;
public class DeleteDirectoryRecursively
{
public static void main(String[] args)
{
String strFolder = "B:\New folder";
deleteRecursive(new File(strFolder));
}
public static void deleteRecursive(File fl)
{
if(!fl.exists())
return;
if(fl.isDirectory())
{
for(File file : fl.listFiles())
{
deleteRecursive(file);
}
}
fl.delete();
System.out.println("Deleted file/directory: " + fl.getAbsolutePath());
}
}
Example 2: delete contents of folder java
String[]entries = index.list();
for(String s: entries){
File currentFile = new File(index.getPath(),s);
currentFile.delete();
}