Here you can find the source of deleteRecursive(final File file, final boolean collect)
private static boolean deleteRecursive(final File file, final boolean collect)
//package com.java2s; /******************************************************************************* * Copyright (c) 2019 Red Hat, Inc.//from w w w .j a v a 2 s . com * Distributed under license by Red Hat, Inc. All rights reserved. * This program is 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 java.io.File; public class Main { private static boolean deleteRecursive(final File file, final boolean collect) { boolean result = true; if (collect && isWindows()) { System.gc(); // ensure no lingering handles that would prevent deletion } File[] children = file.listFiles(); if (children != null) { for (File sf : children) { if (sf.isDirectory()) { if (!deleteRecursive(sf, false)) result = false; } else { if (!sf.delete()) result = false; } } } return file.delete() && result; } private static boolean isWindows() { return System.getProperty("os.name").startsWith("Windows"); //$NON-NLS-1$ //$NON-NLS-2$ } /** * Delete the file or directory, recursively if specified. */ public static boolean delete(File file, final boolean recursive) { boolean result = false; if (recursive) { result = deleteRecursive(file, true); } else { if ((file.listFiles() != null) && (file.listFiles().length != 0)) { throw new RuntimeException("directory not empty"); //$NON-NLS-1$ } if (isWindows()) { System.gc(); // ensure no lingering handles that would prevent // deletion } result = file.delete(); } return result; } }