java遍历指定目录的所有文件,可采用递归和非递归的方式。

定义了接口FileOpera,由具体的实现指定对目录及文件执行的操作。


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;

public class FileUtil {

	/** * 对目录及文件的制定操作接口封装 * * @author jsword * */
	public interface FileOpera {
		void doFile(File file);

		void doFileDir(File file);
	}

	private static FileOpera fileOpera;

	/** * 是否输出测试信息的标志 对于条件表达式中永远为false的语句,编译器将不对条件覆盖的代码段生成字节码。 */
	public static final boolean TEST_FLAG = false;

	/** * 非递归遍历文件夹,依据树的层次遍历的原理,借助队列queue实现 * * @param path * @param visit : 对每个文件进行操作的接口,如果为null,空操作 * @return int[] : 长度为2的数组,第1个为文件夹数量,第2个为文件数量 * @throws IllegalArgumentException * @throws FileNotFoundException */
	public static int[] traverseFolder(String path, FileUtil.FileOpera visit)
			throws FileNotFoundException, IllegalArgumentException {
		checktraverseFolderParams(path);
		
		fileOpera = visit;
		int[] params = new int[] { 0, 0 }; // save folder num(params[0]) and file num(params[1])
		File file = new File(path);
		Queue<File> queue = new LinkedBlockingDeque<>();

		queue.add(file);

		File temp_file;
		while (!queue.isEmpty()) {
			temp_file = queue.remove();
			File[] files = temp_file.listFiles();
			for (File tmpFile : files) {
				if (tmpFile.isDirectory()) {
					if (TEST_FLAG)
						System.out.println("folder :" + tmpFile.getAbsolutePath());
					if (null != fileOpera)
						fileOpera.doFileDir(tmpFile);
					queue.add(tmpFile);
					params[0]++;
				} else {
					if (TEST_FLAG)
						System.out.println("file: " + tmpFile.getAbsolutePath());
					if (null != fileOpera)
						fileOpera.doFile(tmpFile);
					params[1]++;
				}
			}
		}
		return params;
	}

	/** * 递归遍历所有的文件和文件夹 * * @param path : 必须为目录 * @throws IllegalArgumentException * @throws FileNotFoundException */
	public static int[] traverseFolderResusion(String path, FileUtil.FileOpera visit)
			throws FileNotFoundException, IllegalArgumentException {

		checktraverseFolderParams(path);

		fileOpera = visit;
		int[] params = new int[] { 0, 0 }; // save folder num(params[0]) and file num(params[1])
		traverseFolderResusion(new File(path), params);
		return params;
	}

	/** * @param dirFile * @param params */
	private static void traverseFolderResusion(File dirFile, int params[]) {
		File[] files = dirFile.listFiles();
		if (files.length == 0) { // empty folder
			if (TEST_FLAG)
				System.out.println("folder is empty!");
			if (null != fileOpera)
				fileOpera.doFileDir(dirFile);
			return;
		} else {
			for (File tmpFile : files) {
				if (tmpFile.isDirectory()) {
					if (TEST_FLAG)
						System.out.println("folder " + tmpFile.getAbsolutePath());
					if (null != fileOpera)
						fileOpera.doFileDir(tmpFile);
					++params[0];
					traverseFolderResusion(new File(tmpFile.getAbsolutePath()), params);
				} else {
					if (TEST_FLAG)
						System.out.println("file " + tmpFile.getAbsolutePath());
					if (null != fileOpera)
						fileOpera.doFile(tmpFile);
					++params[1];
				}
			}
		}
	}

	private static boolean checktraverseFolderParams(String path)
			throws FileNotFoundException, IllegalArgumentException {
		File scfile = new File(path);
		if (!scfile.exists())
			throw new FileNotFoundException("path :" + path + " is not exist.");
		if (scfile.isFile())
			throw new IllegalArgumentException("path :" + path + " is not directory.");
		return true;
	}

}