Java Delete Directory deleteFiles(File folder)

Here you can find the source of deleteFiles(File folder)

Description

delete Files

License

Apache License

Declaration

public static boolean deleteFiles(File folder) 

Method Source Code


//package com.java2s;
/*/* w w  w . j  av  a  2s .  co  m*/
 * Copyright @ 2006-2010 by The Jxva Framework Foundation
 * 
 * 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.LinkedList;
import java.util.List;

public class Main {
    private static final String FILE = "file";
    private static final String FOLDER = "folder";
    private static final String ALL = "all";

    public static boolean deleteFiles(File folder) {
        if (folder == null)
            return false;
        if (!folder.exists() || !folder.isDirectory()) {
            return false;
        }
        for (File file : getFiles(folder)) {
            file.delete();
        }
        return true;
    }

    public static List<File> getFiles(File folder) {
        return getAll(folder, FILE);
    }

    public static List<File> getAll(File folder) {
        return getAll(folder, ALL);
    }

    private static List<File> getAll(File folder, String type) {
        List<File> list = new LinkedList<File>();
        File flist[] = folder.listFiles();
        for (int i = 0; i < flist.length; i++) {
            if (type.equals(FILE)) {
                if (!flist[i].isDirectory()) {
                    list.add(flist[i]);
                }
            } else if (type.equals(FOLDER)) {
                if (!flist[i].isDirectory()) {
                    list.add(flist[i]);
                }
            } else {
                list.add(flist[i]);
            }

        }
        return list;
    }
}

Related

  1. deleteFiles(File directory, String prefix)
  2. deleteFiles(File docFolder)
  3. deleteFiles(File f, boolean del_self)
  4. deleteFiles(File file)
  5. deleteFiles(File folder)
  6. deleteFiles(File parent, boolean incParent)
  7. deleteFiles(File[] files)
  8. deleteFiles(File[] files)
  9. deleteFiles(File[] intermediateFiles)