Java Delete Directory deleteFilesInDir(File dir)

Here you can find the source of deleteFilesInDir(File dir)

Description

delete Files In Dir

License

Apache License

Parameter

Parameter Description
dir a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void deleteFilesInDir(File dir) throws IOException 

Method Source Code

//package com.java2s;
/*// w  ww  .  java2  s.  c o  m
 *    Copyright 2011 Ken Gilmer
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

import java.io.File;

import java.io.IOException;

import java.util.Arrays;

public class Main {
    /**
     * @param dir
     * @throws IOException
     */
    public static void deleteFilesInDir(File dir) throws IOException {
        validateFile(dir, false, true, false, true);
        for (File f : Arrays.asList(dir.listFiles()))
            if (!f.delete())
                throw new IOException("Unable to delete: " + f);
    }

    /**
     * Determine that a File is as specified by input parameters, throw exception if something does not match.
     * 
     * @param f input file
     * @param createIfNecessary In case of directory, create the directory(s) if don't exist.
     * @param shouldExist fail if does not exist.
     * @param isFile fail if not a file.
     * @param isDirectory fail if not a directory.
     * @throws IOException thrown when input parameters do not match input file.
     */
    public static void validateFile(File f, boolean createIfNecessary,
            boolean shouldExist, boolean isFile, boolean isDirectory)
            throws IOException {
        if (createIfNecessary && !shouldExist)
            throw new IOException(
                    "Invalid parameters: create and !shouldExist");

        if (createIfNecessary && isFile)
            throw new IOException("Invalid parameters: create and isFile.");

        if (createIfNecessary && !f.exists())
            if (!f.mkdirs())
                throw new IOException("Unable to create " + f);

        if (shouldExist && !f.exists())
            throw new IOException(f.getAbsolutePath() + " does not exist.");

        if (!shouldExist && f.exists())
            throw new IOException(f.getAbsolutePath() + " already exists.");

        if (isFile && !f.isFile())
            throw new IOException(f.getAbsolutePath() + " is not a file.");

        if (isDirectory && !f.isDirectory())
            throw new IOException(f.getAbsolutePath()
                    + " is not a directory.");
    }
}

Related

  1. deleteFiles(String sDir, String sSearch)
  2. deleteFiles(String[] filenames)
  3. deleteFilesAndDir(List files)
  4. deleteFilesAndSubDirs(File dir)
  5. deleteFilesFromDirectory(final String dirNameIn, final String filePrefixIn)
  6. deleteFilesInDir(File dir)