Here you can find the source of removeDirectoryIfItIsEmpty(Path directoryToRemove)
Parameter | Description |
---|---|
directoryToRemove | directory which is verified for emptiness |
Parameter | Description |
---|---|
IOException | an exception |
public static void removeDirectoryIfItIsEmpty(Path directoryToRemove) throws IOException
//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); } } }