懒得说了,直接看代码吧。
package org.ursob.util; import java.io.File; public class FileUtil { public FileUtil() { } /** * 获得目录下文件及目录的个数 * @param File f * @return * @throws Exception */ public static Integer getFileCount(File f) throws Exception { Integer size = 0; File flist[] = f.listFiles(); FOR (int i = 0; flist != NULL && i < flist.length; i++) { IF (flist[i].isDirectory()) { continue; } else { size += flist[i].length(); } } RETURN size; } /** * 获得目录下文件及目录的个数 * @param File f * @return * @throws Exception */ public static Integer getPathSize(File f) throws Exception { File[] files = f.listFiles(); RETURN files == NULL ? 0 : files.length; } /**递归删除文件 * @param File file * @return */ public static BOOLEAN delFiles(File file) { BOOLEAN flag = false; try { IF (file.EXISTS()) { IF (file.isDirectory()) { String[] contents = file.list(); FOR (int i = 0; i < contents.length; i++) { File tempFile = new File (file.getAbsolutePath() + "/"+ contents[i]); IF (tempFile.EXISTS()) { IF (tempFile.isFile()) { flag = tempFile.DELETE(); } else IF (tempFile.isDirectory()) { delFiles(tempFile); } } else { throw new RuntimeException("File not exist!"); } } } flag = file.DELETE(); } else { throw new RuntimeException("File not exist!"); } } catch (Exception e) { flag = false; e.printStackTrace(); } RETURN flag; } /** * 移动指定文件夹内的全部文件 * @param fromDir 待移动的文件 * @param toDir 目标文件目录 * @throws Exception */ public static void fileMove(String FROM, String TO) throws Exception { try { File dir = new File(FROM); // 文件一览 File[] files = dir.listFiles(); IF (files == NULL) RETURN; // 目标 File moveDir = new File(TO); IF (!moveDir.EXISTS()) { moveDir.mkdirs(); } // 文件移动 FOR (int i = 0; i < files.length; i++) { IF (files[i].isDirectory()) { fileMove(files[i].getPath(), TO + "\\" + files[i].getName()); // 成功,删除原文件 files[i].DELETE(); } File moveFile = new File(moveDir.getPath() + "\\" + files[i].getName()); // 目标文件夹下存在的话,删除 IF (moveFile.EXISTS()) { moveFile.DELETE(); } files[i].renameTo(moveFile); } } catch (Exception e) { throw e; } } }