Java tutorial
//package com.java2s; import java.io.File; public class Main { /** * Delete corresponding path, file or directory. * * @param file path to delete. */ public static void delete(File file) { delete(file, false); } /** * Delete corresponding path, file or directory. * * @param file path to delete. * @param ignoreDir whether ignore directory. If true, all files will be deleted while directories is reserved. */ public static void delete(File file, boolean ignoreDir) { if (file == null || !file.exists()) { return; } if (file.isFile()) { file.delete(); return; } File[] fileList = file.listFiles(); if (fileList == null) { return; } for (File f : fileList) { delete(f, ignoreDir); } // delete the folder if need. if (!ignoreDir) file.delete(); } }