java文件夹遍历小工具

文件夹遍历在程序中经常用到, 写了个小程序, 免得每次都重新写, 太麻烦了!


/**
 * 
 */
package com.qefee.tool.io;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;

import com.qefee.tool.io.command.FileCommand;
import com.qefee.tool.io.command.FolderCommand;

/**
 * @author qefee
 * 
 */
public class FolderWalker {

	/**
	 * root folder first? sub folder first?
	 * 
	 * @author qefee
	 * 
	 */
	public enum WalkOrder {
		RootFolerFirst, SubFolderFirst
	}

	/**
	 * file first? folder first?
	 * 
	 * @author qefee
	 * 
	 */
	public enum FileOrder {
		FileFirst, FolderFirst
	}

	/**
	 * walk.
	 * 
	 * @param folderPath
	 * @param fileCommand
	 * @param folderCommand
	 * @param walkOrder
	 * @param fileOrder
	 * @throws IOException
	 */
	public static void walk(String folderPath, FileCommand fileCommand,
			FolderCommand folderCommand, WalkOrder walkOrder,
			FileOrder fileOrder) throws IOException {
		walk(new File(folderPath), fileCommand, folderCommand, walkOrder,
				fileOrder);
	}

	/**
	 * walk.
	 * 
	 * @param folder
	 * @param fileCommand
	 * @param folderCommand
	 * @param walkorder
	 * @param fileOrder
	 * @throws IOException
	 */
	public static void walk(File folder, FileCommand fileCommand,
			FolderCommand folderCommand, WalkOrder walkorder,
			FileOrder fileOrder) throws IOException {
		if (folder == null) {
			throw new IOException("folder argument is null.");
		}

		if (!folder.isDirectory()) {
			throw new IOException(folder.getAbsolutePath()
					+ " is not a folder.");
		}

		File[] listFiles = folder.listFiles();
		LinkedList<File> folderList = new LinkedList<File>();
		LinkedList<File> fileList = new LinkedList<File>();

		for (File f : listFiles) {
			if (f.isDirectory()) {
				folderList.add(f);
			} else {
				fileList.add(f);
			}
		}

		switch (fileOrder) {
		case FileFirst:
			handleFile(fileCommand, fileList);
			handleFolder(fileCommand, folderCommand, walkorder, fileOrder,
					folderList);
			break;

		default:
			handleFolder(fileCommand, folderCommand, walkorder, fileOrder,
					folderList);
			handleFile(fileCommand, fileList);
			break;
		}
	}

	/**
	 * handle folder.
	 * 
	 * @param fileCommand
	 * @param folderCommand
	 * @param walkorder
	 * @param fileOrder
	 * @param folderList
	 * @throws IOException
	 */
	private static void handleFolder(FileCommand fileCommand,
			FolderCommand folderCommand, WalkOrder walkorder,
			FileOrder fileOrder, LinkedList<File> folderList)
			throws IOException {
		for (File fo : folderList) {
			switch (walkorder) {
			case RootFolerFirst:
				if (folderCommand != null) {
					folderCommand.exexute(fo);
				}
				walk(fo, fileCommand, folderCommand, walkorder, fileOrder);
				break;

			default:
				walk(fo, fileCommand, folderCommand, walkorder, fileOrder);
				if (folderCommand != null) {
					folderCommand.exexute(fo);
				}
				break;
			}
		}
	}

	/**
	 * handle file.
	 * 
	 * @param fileCommand
	 * @param fileList
	 * @throws IOException
	 */
	private static void handleFile(FileCommand fileCommand,
			LinkedList<File> fileList) throws IOException {
		if (fileCommand == null) {
			return;
		}

		for (File file : fileList) {
			fileCommand.exexute(file);
		}
	}
}

/**
 * 
 */
package com.qefee.tool.io.command;

import java.io.File;
import java.io.IOException;

/**
 * the command of handle file.
 * 
 * @author qefee
 * 
 */
public abstract class FileCommand implements ICommand {

	@Override
	public void exexute(File file) throws IOException {
		if (!file.isFile()) {
			throw new IOException(file.getAbsolutePath() + " is not a file.");
		}

		handle(file);
	}
}

/**
 * 
 */
package com.qefee.tool.io.command;

import java.io.File;
import java.io.IOException;

/**
 * the command of handle folder.
 * 
 * @author qefee
 * 
 */
public abstract class FolderCommand implements ICommand {

	@Override
	public void exexute(File folder) throws IOException {
		if (!folder.isDirectory()) {
			throw new IOException(folder.getAbsolutePath()
					+ " is not a folder.");
		}

		handle(folder);
	}
}

/**
 * 
 */
package com.qefee.tool.io.command;

import java.io.File;
import java.io.IOException;

/**
 * @author qefee
 * 
 */
public interface ICommand {
	/**
	 * execute command.
	 * 
	 * @param file
	 * @throws IOException
	 */
	void exexute(File file) throws IOException;

	/**
	 * handle file/folder
	 * 
	 * @param file
	 * @throws IOException
	 */
	void handle(File file) throws IOException;
}

使用法术如下代码

/**
 * 
 */
package com.qefee.tool.io;

import java.io.File;
import java.io.IOException;

import com.qefee.tool.io.command.FileCommand;
import com.qefee.tool.io.command.FolderCommand;

/**
 * @author qefee
 * 
 */
public class App {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FileCommand fileCommand = new FileCommand() {

			@Override
			public void handle(File file) throws IOException {
				System.out.println("File   : " + file.getAbsolutePath());
			}
		};

		FolderCommand folderCommand = new FolderCommand() {

			@Override
			public void handle(File file) throws IOException {
				System.out.println("Folder : " + file.getAbsolutePath());

			}
		};

		try {
			System.out.println("#############################");
			FolderWalker.walk(".", fileCommand, folderCommand,
					FolderWalker.WalkOrder.RootFolerFirst,
					FolderWalker.FileOrder.FileFirst);
			
			
			System.out.println("#############################");
			FolderWalker.walk(".", fileCommand, folderCommand,
					FolderWalker.WalkOrder.RootFolerFirst,
					FolderWalker.FileOrder.FolderFirst);
			
			
			System.out.println("#############################");
			FolderWalker.walk(".", fileCommand, folderCommand,
					FolderWalker.WalkOrder.SubFolderFirst,
					FolderWalker.FileOrder.FileFirst);
			
			
			System.out.println("#############################");
			FolderWalker.walk(".", fileCommand, folderCommand,
					FolderWalker.WalkOrder.SubFolderFirst,
					FolderWalker.FileOrder.FolderFirst);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值