Java Delete Directory deleteDirectoryContents(final File dir, int deleteDirLevel, int level)

Here you can find the source of deleteDirectoryContents(final File dir, int deleteDirLevel, int level)

Description

delete Directory Contents

License

Open Source License

Declaration

public static final void deleteDirectoryContents(final File dir, int deleteDirLevel, int level) 

Method Source Code

//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
        }
    }
}

Related

  1. deleteDirectoryContents(File directory)
  2. deleteDirectoryContents(File directory)
  3. deleteDirectoryContents(File directory)
  4. deleteDirectoryContents(File directory)
  5. deleteDirectoryContents(final File dir)
  6. deleteFiles(File dir)
  7. deleteFiles(File dir)
  8. deleteFiles(File directory)
  9. deleteFiles(File directory)