Java File Path Delete deleteAllFiles(File directory)

Here you can find the source of deleteAllFiles(File directory)

Description

Dlete all files and directory from directory

License

Apache License

Parameter

Parameter Description
directory Directory

Declaration

public static void deleteAllFiles(File directory) 

Method Source Code

//package com.java2s;
/*//from   ww  w.  ja  v a 2s . c o  m
 * Copyright 2015 ROLLUS Lo?c
 *
 * 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.util.List;

public class Main {
    /**
     * Dlete all files and directory from directory
     * @param directory Directory
     */
    public static void deleteAllFiles(File directory) {
        File[] fs = directory.listFiles();
        if (fs != null) {
            for (int i = 0; i < fs.length; i++) {
                fs[i].delete();
            }
        }
    }

    /**
     * List all file from file/directory f (and its subdirectories) in list v
     * Don't list picture in cbirthumb directory (thumbnail directory)
     * @param f File f
     * @param v Destination list
     */
    public static void listFiles(File f, List<String> v) {

        File[] fs = f.listFiles();
        for (int i = 0; i < fs.length; i++) {
            if (fs[i].isDirectory()) {
                //directory which contain no thumb, list its files
                listFiles(fs[i], v);
            } else {
                //its a file
                v.add(fs[i].getAbsolutePath());
            }
        }

    }

    /**
     * List all file from file/directory f (and its subdirectories) in list v
     * Don't list picture in cbirthumb directory (thumbnail directory)
     * @param f File f
     * @param v Destination list
     * @param format Accepted format
     */
    public static void listFiles(File f, List<String> v, String[] format) {
        File[] fs = f.listFiles();
        for (int i = 0; i < fs.length; i++) {
            if (fs[i].isDirectory()) {
                //directory which contain no thumb, list its files
                listFiles(fs[i], v, format);
            } else {
                //its a file
                if (isValidFormat(fs[i], format)) {
                    v.add(fs[i].getAbsolutePath());
                }
            }
        }
    }

    private static boolean isValidFormat(File f, String[] format) {
        boolean valid = false;
        String formatFile = getUpperCaseFormat(f);
        for (String allowedFormat : format) {
            if (formatFile.equals(allowedFormat.toUpperCase())) {
                valid = true;
            }
        }
        return valid;
    }

    private static String getUpperCaseFormat(File f) {
        String name = f.getName().toUpperCase();
        int pos = name.lastIndexOf('.');
        if (pos == -1 || name.length() < (pos + 1)) {
            return "";
        }
        String ext = name.substring(pos + 1);
        return ext;
    }
}

Related

  1. deleteAllFile(String folderPath)
  2. deleteAllFile(String path)
  3. deleteAllFile(String path)
  4. deleteAllFiles()
  5. deleteAllFiles(File dir)
  6. deleteAllFiles(File directory)
  7. deleteAllFiles(File dirOrFile)
  8. deleteAllFiles(String directory)
  9. deleteAllFiles(String folder, String type)