Here you can find the source of deleteAllFiles(File dirOrFile)
Parameter | Description |
---|---|
dirOrFile | - the File object which could be either a folder or a file. |
public static void deleteAllFiles(File dirOrFile)
//package com.java2s; /******************************************************************************* * Copyright (c) 2004,2009 Actuate Corporation. * 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:/*w w w . j a v a 2 s . c om*/ * Actuate Corporation - initial API and implementation *******************************************************************************/ import java.io.File; public class Main { /** * Recursively delete all the files and folders under dirOrFile * * @param dirOrFile - * the File object which could be either a folder or a file. */ public static void deleteAllFiles(File dirOrFile) { if (!dirOrFile.exists()) return; if (dirOrFile.isFile()) { dirOrFile.delete(); } else // dirOrFile is directory { if ((dirOrFile.listFiles() != null) && (dirOrFile.listFiles().length > 0)) { File[] fileList = dirOrFile.listFiles(); for (int i = 0; i < fileList.length; i++) deleteAllFiles(fileList[i]); } // Directory can only be deleted when it is empty. dirOrFile.delete(); } } }