Here you can find the source of delete(File dir)
Parameter | Description |
---|---|
dir | the File file or directory to delete. |
public static boolean delete(File dir)
//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; } }