java递归删除目录的一个小问题

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DeleteFilesDemo {

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    File dir = new File("C:\\CODE\\a");
    deleteDir(dir);
    printTime();
}

public static void deleteDir(File dir) {
    File[] files = dir.listFiles();
    for (File f : files) {
        if (f.isDirectory())
            deleteDir(f);
        else
            System.out.println(f.toString() + "——" + f.delete());
    }
    System.out.println(dir.toString() + "................" + dir.delete());
}

public static void printTime() throws FileNotFoundException {
    String time = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss").format(new Date());
    new PrintStream("c:/code/deleteTime.log").print(time + "\r\n所有目录与文件删除成功!");
}

}

请问,如果递归到一个空目录,再调用一次deleteDir得到的File数组不就是空的了吗?那么遍历的时候为什么不会出现异常呢?

这里假设是空目录A,判断是目录,递归调用deleteDir(File dir),然后到了 File[] files = dir.listFiles();这句,由于是空目录所以files=null;所以不进for循环的,就直接删除了。

先删除子目录,再删除此目录,怎么会遍历到空目录

不是你想的那样。
File[] files = dir.listFiles();
如果files为空及一个空目录,程序就进不了for循环,所有就调不到deleteDir(f)就不会出现你想的那种情况。所有不会抛异常。

foreach语句,空集不会迭代。

 /**
     * Returns an array of abstract pathnames denoting the files in the
     * directory denoted by this abstract pathname.
     *
     * <p> If this abstract pathname does not denote a directory, then this
     * method returns {@code null}.  Otherwise an array of {@code File} objects
     * is returned, one for each file or directory in the directory.  Pathnames
     * denoting the directory itself and the directory's parent directory are
     * not included in the result.  Each resulting abstract pathname is
     * constructed from this abstract pathname using the {@link #File(File,
     * String) File(File,&nbsp;String)} constructor.  Therefore if this
     * pathname is absolute then each resulting pathname is absolute; if this
     * pathname is relative then each resulting pathname will be relative to
     * the same directory.
     *
     * <p> There is no guarantee that the name strings in the resulting array
     * will appear in any specific order; they are not, in particular,
     * guaranteed to appear in alphabetical order.
     *
     * <p> Note that the {@link java.nio.file.Files} class defines the {@link
     * java.nio.file.Files#newDirectoryStream(Path) newDirectoryStream} method
     * to open a directory and iterate over the names of the files in the
     * directory. This may use less resources when working with very large
     * directories.
     *
     * @return  An array of abstract pathnames denoting the files and
     *          directories in the directory denoted by this abstract pathname.
     *          The array will be empty if the directory is empty.  Returns
     *          {@code null} if this abstract pathname does not denote a
     *          directory, or if an I/O error occurs.
     *
     * @throws  SecurityException
     *          If a security manager exists and its {@link
     *          SecurityManager#checkRead(String)} method denies read access to
     *          the directory
     *
     * @since  1.2
     */
    public File[] listFiles() {
        String[] ss = list();
        if (ss == null) return null;
        int n = ss.length;
        File[] fs = new File[n];
        for (int i = 0; i < n; i++) {
            fs[i] = new File(ss[i], this);
        }
        return fs;
    }

return里写了,The array will be empty if the directory is empty.