Java Directory Clear cleanDirectory(final File directory)

Here you can find the source of cleanDirectory(final File directory)

Description

Clean a directory without deleting it.

License

Apache License

Declaration

private static void cleanDirectory(final File directory) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**/*from   www  .j a  va 2 s  .co m*/
     * Clean a directory without deleting it.
     */
    private static void cleanDirectory(final File directory) throws IOException {
        if (!directory.exists()) {
            return;
        }

        if (!directory.isDirectory()) {
            return;
        }

        IOException exception = null;

        final File[] files = directory.listFiles();
        if (files != null) {
            for (final File file : files) {
                try {
                    cleanDirectory(file);
                    if (!file.delete()) {
                        throw new IOException("Cannot delete " + file.getAbsolutePath());
                    }
                } catch (final IOException ioe) {
                    exception = ioe;
                }
            }
        }

        if (null != exception) {
            throw exception;
        }
    }
}

Related

  1. cleanDirectory(File directory)
  2. cleanDirectory(File directory)
  3. cleanDirectory(File directory)
  4. cleanDirectory(File path)
  5. cleanDirectory(final File directory)
  6. cleanDirectory(IProgressMonitor monitor, File directory)
  7. cleanDirectory(IProgressMonitor monitor, File directory, File base, int step)
  8. cleanDirectory(String dir)
  9. cleanDirectory(String dirPath)