Here you can find the source of delTree(File dir)
Parameter | Description |
---|---|
dir | a parameter |
public static boolean delTree(File dir)
//package com.java2s; /*//from www . j av a2 s. c o m * Copyright (c) Fiorano Software Pte. Ltd. and affiliates. All rights reserved. http://www.fiorano.com * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */ import java.io.*; public class Main { /** * @param dir * @return */ public static boolean delTree(File dir) { String[] fileList = dir.list(); String path = dir.getAbsolutePath() + File.separator; File file; for (int i = 0; i < fileList.length; ++i) { file = new File(path + fileList[i]); if (file.isDirectory()) delTree(file); else file.delete(); } return dir.delete(); } /** * Delete the directory tree specified by parameter directory name. * * @param directoryName directory that need to be deleted * @param maxAttempts maximum attempts that can be made for file deletion * @param timeInterval specfies the sleep time after unsuccessful deletion attempt */ public static void delTree(String directoryName, int maxAttempts, long timeInterval) { File temp = new File(directoryName); if (!temp.isDirectory()) return; String[] subDirs = temp.list(); for (int i = 0; i < subDirs.length; i++) { File tempFile = new File(directoryName + File.separator + subDirs[i]); if (tempFile.isDirectory()) { delTree(directoryName + File.separator + subDirs[i], maxAttempts, timeInterval); continue; } boolean deleted; int j = 0; while (j < maxAttempts) { deleted = tempFile.delete(); try { if (!deleted) Thread.sleep(timeInterval); else break; } catch (InterruptedException ie) { RuntimeException re = new RuntimeException("error.while.deleting.the.tree"); re.initCause(ie); throw re; } j++; } } temp.delete(); } }