Android examples for java.io:Directory
deletes a file or recursively deletes directory
import android.annotation.SuppressLint; import android.content.ClipDescription; import android.content.Context; import android.net.Uri; import android.os.StatFs; import android.util.Log; import android.webkit.URLUtil; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.DateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; public class Main{ /**/*w w w. java2 s . c o m*/ * deletes a file or recursively deletes directory * @param topFile - the file or directory to delete * @throws IOException */ public static void deleteRecursively(File topFile) throws IOException { if (topFile.isFile()) { if (!topFile.delete()) { throw new IOException("Cannot delete file " + topFile.getAbsolutePath()); } return; } File[] fileArray = topFile.listFiles(); if (fileArray == null) { if (!topFile.delete()) { throw new IOException("Cannot delete directory " + topFile.getAbsolutePath()); } return; } for (File file : fileArray) { if (Thread.currentThread().isInterrupted()) { break; } if (file.isFile()) { if (!file.delete()) { throw new IOException("Cannot delete file " + file.getAbsolutePath()); } continue; } if (file.isDirectory()) { deleteRecursively(file); } } if (!topFile.delete()) { throw new IOException("Cannot delete directory " + topFile.getAbsolutePath()); } } }