Create a method that deletes a non-empty directory - Java File Path IO

Java examples for File Path IO:Directory Create

Description

Create a method that deletes a non-empty directory

Demo Code

import java.io.File;
import java.io.IOException;

public class Main {
  public static void main(String[] arg) throws IOException {
    File f = new File("myfile.txt");
    deleteFile(f);//w w w  .ja v  a 2s . co  m
  }

  private static void deleteFile(File dir) {
    File[] files = dir.listFiles();
    for (File f : files) {
      if (f.isDirectory())
        deleteFile(f);
      else
        f.delete();
    }
    dir.delete();
  }
}

Related Tutorials