Here you can find the source of deleteDirectoryContents(final File dir, int deleteDirLevel, int level)
public static final void deleteDirectoryContents(final File dir, int deleteDirLevel, int level)
//package com.java2s; /******************************************************************************* * Copyright (c) 2016 Weasis Team and others. * 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:/* www .j a va2 s. c o m*/ * Nicolas Roduit - initial API and implementation *******************************************************************************/ import java.io.File; public class Main { public static final void deleteDirectoryContents(final File dir, int deleteDirLevel, int level) { if ((dir == null) || !dir.isDirectory()) { return; } final File[] files = dir.listFiles(); if (files != null) { for (final File f : files) { if (f.isDirectory()) { deleteDirectoryContents(f, deleteDirLevel, level + 1); } else { deleteFile(f); } } } if (level >= deleteDirLevel) { deleteFile(dir); } } private static void deleteFile(File fileOrDirectory) { try { fileOrDirectory.delete(); } catch (Exception e) { // Do nothing, wait next start to delete it } } }