Here you can find the source of delete(File file)
public static void delete(File file)
//package com.java2s; /******************************************************************************* * Copyright (c) 1998, 2013 Oracle and/or its affiliates. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 * which accompanies this distribution. * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * * Contributors:// w w w. ja v a 2s. c o m * Oracle - initial API and implementation from Oracle TopLink ******************************************************************************/ import java.io.File; public class Main { public static void delete(File file) { if (!file.exists()) { return; } if (file.isDirectory()) { String[] entries = file.list(); if (entries.length == 0) { file.delete(); } else { for (int i = 0; i < entries.length; i++) { delete(new File(file, entries[i])); } // delete directory after its containing files were deleted if (file.list().length == 0) { file.delete(); } } } else { file.delete(); } } }