文件夹遍历在程序中经常用到, 写了个小程序, 免得每次都重新写, 太麻烦了!
/**
*
*/
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();
}
}
}