Java File Path Delete deleteAllFilesInDirectory(String path)

Here you can find the source of deleteAllFilesInDirectory(String path)

Description

deletes entire contents of a folder.

License

Open Source License

Declaration

public static boolean deleteAllFilesInDirectory(String path) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    /**/*from  w  w w  .ja va 2  s  .  c  om*/
    * deletes entire contents of a folder. Returns true if deletion successful, false otherwise
    *
    * comment added by thomas:
    * doesn't delete folders that contain nonempty folders
    */
    public static boolean deleteAllFilesInDirectory(String path) {
        File folder = new File(path);
        if (folder != null && folder.exists() && folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    files[i].delete();
                }
            }
            return true;
        }
        return false;
    }

    public static boolean exists(String path, String fileNameWithoutFullPath) {
        String filePath = getFileNameWithPath(path, fileNameWithoutFullPath);
        return exists(filePath);
    }

    public static boolean exists(String fileNameWithFullPath) {
        File file = new File(fileNameWithFullPath);
        return file.exists();
    }

    /**
     * Deletes a File Object.
     */
    public static File delete(File file) {
        file.delete();
        return file;
    }

    /**
     * Deletes a File from an url.
     */

    public static boolean delete(String fileNameWithFullPath) {
        File file = new File(fileNameWithFullPath);
        return file.delete();
    }

    public static String getFileNameWithPath(String path, String fileNameWithoutFullPath) {
        return path + File.separator + fileNameWithoutFullPath;
    }
}

Related

  1. deleteAllFiles(String folder, String type)
  2. deleteAllFiles(Vector filesToDelete)
  3. deleteAllFilesAux(String input, File swapFolder)
  4. deleteAllFilesIn(String dir)
  5. deleteAllFilesInDir(File dir)
  6. deleteAllFilesOnlyInDirectory(File startingDir)
  7. deleteAllFilesRecursively(String directoryName)
  8. deleteAndCreateFolder(String path)
  9. deleteBlankPath(String path)