Android File Delete deleteFilesRecursive(File src)

Here you can find the source of deleteFilesRecursive(File src)

Description

delete all the files in a directory recursively

Parameter

Parameter Description
src a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void deleteFilesRecursive(File src) throws IOException 

Method Source Code

//package com.java2s;
import java.io.*;

public class Main {
    /**//from   w w w  .  j  av a 2s .c o  m
     * delete all the files in a directory recursively
     *
     * @param src
     * @throws IOException
     */
    public static void deleteFilesRecursive(File src) throws IOException {
        // Check to ensure that the source is valid...
        if (!src.exists()) {
            if (!src.mkdirs()) {
                throw new IOException(
                        "deleteFiles: Could not create direcotry: "
                                + src.getAbsolutePath() + ".");
            }
        } else if (!src.canRead()) { // check to ensure we have rights to the source...
            throw new IOException("copyFiles: No right to source: "
                    + src.getAbsolutePath() + ".");
        }
        // is this a directory copy?
        if (src.isDirectory()) {
            // get a listing of files...
            String list[] = src.list();
            // copy all the files in the list.
            for (int i = 0; i < list.length; i++) {
                File src1 = new File(src, list[i]);
                deleteFilesRecursive(src1);
            }
        } else {
            src.delete();
        }
    }
}

Related

  1. deleteFile(String path)
  2. deleteFile(String strSrc)
  3. deleteFileOnly(String path)
  4. deleteFiles(File file)
  5. deleteFiles(File file, String regex)
  6. deleteFolder(File targetFolder)
  7. deleteFolders(File dir)
  8. deleteIfExists(File file)
  9. deleteOldFile(String strPath, String strWildcard, int iOffset)