1. 程式人生 > >檔案工具類FileUtil

檔案工具類FileUtil

package com.yung.ppapi.util;

import java.io.File;

public class FileUtil {

	/**
	 * 刪除檔案
	 * 
	 * @param fileName
	 * @return
	 */
	public static boolean deleteFile(String fileName) {
		return deleteFile(new File(fileName));
	}

	/**
	 * 刪除檔案,支援遞迴刪除目錄下的所有檔案及子目錄下所有檔案
	 * 
	 * @param dir 將要刪除的檔案目錄
	 * @return boolean Returns "true" if all deletions were successful. If a
	 *         deletion fails, the method stops attempting to delete and returns
	 *         "false".
	 */
	public static boolean deleteFile(File file) {
		if (file.isDirectory()) {
			String[] children = file.list();
			// 遞迴刪除目錄中的子目錄下
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteFile(new File(file, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		// 目錄此時為空,可以刪除
		return file.delete();
	}

//  public static void main(String[] args) {
//		System.out.println(deleteDir("D:\\tmp\\zip"));
//	}

}

樓主這麼辛苦,請使用樓主的推薦碼領取支付寶紅包吧,記得一定要消費掉哦。雙贏^_^。

1、開啟支付寶首頁搜尋“8282987” 立即領紅包。