Java Path Remove nio removeDirectoryIfItIsEmpty(Path directoryToRemove)

Here you can find the source of removeDirectoryIfItIsEmpty(Path directoryToRemove)

Description

Checks if directoryToRemove is empty itself and remove it if it is empty only.

License

Open Source License

Parameter

Parameter Description
directoryToRemove directory which is verified for emptiness

Exception

Parameter Description
IOException an exception

Declaration

public static void removeDirectoryIfItIsEmpty(Path directoryToRemove) throws IOException 

Method Source Code


//package com.java2s;
/*//  w w w .j  a  v a  2 s. c  om
 * Copyright (c) 2012-2018 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */

import static java.util.stream.Collectors.toList;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    /**
     * Checks if directoryToRemove is empty itself and remove it if it is empty only.
     *
     * @param directoryToRemove directory which is verified for emptiness
     * @throws IOException
     */
    public static void removeDirectoryIfItIsEmpty(Path directoryToRemove) throws IOException {
        if (Files.exists(directoryToRemove) && Files.isDirectory(directoryToRemove)
                && Files.list(directoryToRemove).collect(toList()).isEmpty()) {

            Files.delete(directoryToRemove);
        }
    }
}

Related

  1. appendOrRemove(LinkedList paths, DirectoryStream ds)
  2. recursiveRemoveFolder(Path folderPath)
  3. removeDirectory(Path directory)
  4. removeDirectory(Path directory)
  5. removeDirectory(String pathToDir)
  6. removeDriveLetter(Path path)
  7. removeFile(final String removePath)
  8. removeFile(String path)
  9. removeFile(String workspacePath)