Java Delete File Recursively deleteRecursive(File f)

Here you can find the source of deleteRecursive(File f)

Description

Recursively delete a directory / file For safety this method only deletes files created under the workspace

License

Open Source License

Parameter

Parameter Description
file a parameter

Declaration

private static final void deleteRecursive(File f) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2011 Andrew Gvozdev 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://from  ww  w.  j a v a2 s.c om
 *     Andrew Gvozdev - Initial API and implementation
 *     James Blackburn (Broadcom Corp.)
 *     Liviu Ionescu - bug 392416
 *******************************************************************************/

import java.io.File;

import org.eclipse.core.resources.ResourcesPlugin;

public class Main {
    /**
     * Recursively delete a directory / file
     *
     * For safety this method only deletes files created under the workspace
     *
     * @param file
     */
    private static final void deleteRecursive(File f) throws IllegalArgumentException {
        // Ensure that the file being deleted is a child of the workspace
        // root to prevent anything nasty happening
        if (!f.getAbsolutePath()
                .startsWith(ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().getAbsolutePath())) {
            throw new IllegalArgumentException("File must exist within the workspace!");
        }

        if (f.isDirectory()) {
            for (File f1 : f.listFiles()) {
                deleteRecursive(f1);
            }
        }
        f.delete();
    }
}

Related

  1. deleteRecursive(File dir)
  2. deleteRecursive(File dir)
  3. deleteRecursive(File dir)
  4. deleteRecursive(File f)
  5. deleteRecursive(File f)
  6. deleteRecursive(File f)
  7. deleteRecursive(File file)
  8. deleteRecursive(File file)
  9. deleteRecursive(File file)