Java File Delete delete(File f)

Here you can find the source of delete(File f)

Description

Recursive delete.

License

Academic Free License

Declaration

public static void delete(File f) throws IOException 

Method Source Code

//package com.java2s;
// Licensed under the Academic Free License version 3.0

import java.io.*;

public class Main {
    public static final File[] none = new File[0];

    /**//from w  ww.ja  va2 s.  c om
     * Recursive delete.  Actually throw IOException on error.
     */
    public static void delete(File f) throws IOException {
        if (!f.exists())
            return;

        if (f.isDirectory()) {
            File[] kids = list(f);
            for (int i = 0; kids != null && i < kids.length; ++i)
                delete(kids[i]);
        }

        if (!f.delete())
            throw new IOException("Cannot delete: " + f);
    }

    /**
     * Same as File.listFiles()
     */
    public static File[] list(File f) {
        String[] names = f.list();
        if (names == null || names.length == 0)
            return none;
        File[] files = new File[names.length];
        for (int i = 0; i < names.length; ++i)
            files[i] = new File(f, names[i]);
        return files;
    }
}

Related

  1. delete(File classDir)
  2. delete(File current)
  3. delete(File dir)
  4. delete(File dir)
  5. delete(File dir)
  6. delete(File f)
  7. delete(File f)
  8. delete(File f)
  9. delete(File f)