Java File Delete delete(File dir)

Here you can find the source of delete(File dir)

Description

Delete file or directory.

License

Open Source License

Parameter

Parameter Description
dir the File file or directory to delete.

Return

true if all contents of dir were deleted

Declaration

public static boolean delete(File dir) 

Method Source Code


//package com.java2s;
/* *******************************************************************
 * Copyright (c) 1999-2001 Xerox Corporation, 
 *               2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved. /*from  w w  w  . ja  va2 s  . c om*/
 * 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: 
 *     Xerox/PARC     initial implementation 
 * ******************************************************************/

import java.io.*;

public class Main {
    /** 
     * Delete file or directory.
     * @param dir the File file or directory to delete.
     * @return true if all contents of dir were deleted 
     */
    public static boolean delete(File dir) {
        return deleteContents(dir) && dir.delete();
    }

    /** 
     * Delete contents of directory.
     * The directory itself is not deleted.
     * @param dir the File directory whose contents should be deleted.
     * @return true if all contents of dir were deleted 
     */
    public static boolean deleteContents(File dir) {
        if ((null == dir) || !dir.canWrite()) {
            return false;
        } else if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (!deleteContents(files[i]) || !files[i].delete()) {
                    return false;
                }
            }
        }
        return true;
    }
}

Related

  1. clearFile(String pFileName)
  2. clearFileOrDir(File file)
  3. delete(File aFile)
  4. delete(File classDir)
  5. delete(File current)
  6. delete(File dir)
  7. delete(File dir)
  8. delete(File f)
  9. delete(File f)