Java Folder Delete clearFolder(String directoryLocation)

Here you can find the source of clearFolder(String directoryLocation)

Description

Deleet all the files from the specified location

License

Apache License

Parameter

Parameter Description
directoryLocation a parameter

Declaration

public static void clearFolder(String directoryLocation) 

Method Source Code

//package com.java2s;
/**/*from  w  w  w.j a v a 2 s . c o  m*/
 *  Copyright 2011 Wordnik, Inc.
 *
 *  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.*;

public class Main {
    /**
     * Deleet all the files from the specified location
     * @param directoryLocation
     */
    public static void clearFolder(String directoryLocation) {
        File fDir = new File(directoryLocation);
        File[] files = fDir.listFiles();
        if (files != null) {
            for (File aFile : files) {
                aFile.delete();
            }
        }
    }

    public static void clearFolder(String strFolder, final String strExt) {
        File fLogDir = new File(strFolder);
        File[] fLogs = fLogDir.listFiles(new FilenameFilter() {
            public boolean accept(File fDir, String strName) {
                return (strName.endsWith(strExt));
            }
        });
        if (fLogs != null) {
            for (int i = 0; i < fLogs.length; i++) {
                deleteFile(fLogs[i].getAbsolutePath());
            }
        }
    }

    /**
     * Deletes a fingle file and returns false fi file doesn't exists
     * @param sFilePath
     * @return
     */
    public static boolean deleteFile(String sFilePath) {
        File oFile = new File(sFilePath);
        if (!oFile.exists()) {
            return false;
        }
        return oFile.delete();
    }
}

Related

  1. clearDir(File dir)
  2. clearFiles(File dir)
  3. clearFilesOnPath(String path)
  4. clearFolder(File f)
  5. clearFolder(File[] children)
  6. clearFolder(String folder)
  7. clearFolder(String path)
  8. clearFolderContent(String folderName)
  9. deepDelete(final File file)