Java读取文件夹下的指定类型的文件 递归筛选 包含子文件夹

package cn.com.cloud.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
 * 读取目录及子目录下指定文件名的路径 并放到一个数组里面返回遍历
 * 
 * @author HenryLee
 * 
 */
public class FileViewer {

	/**
	 * 根据文件夹路径,读取所有子目录里包含指定类型的文件。
	 * @param dirPath	
	 * <li>eg:E:\app\agreeWeb\workagree\fly.info\context\images\fly\信息发布\2012\02\08\02\32\27\227\89857567\</li>
	 * @param fileTypes	
	 * <li>eg:jpg,gif,jpeg,bmp</li>
	 * @return 所有符合指定类型文件的文件路径	
	 * <li>eg:E:\app\agreeWeb\workagree\fly.info\context\images\fly\信息发布\2012\02\08\02\32\27\227\89857567\1-1\1-1-2\1.gif</li>
	 */
	public static List<String> getAllFilePathByDir_FilterByFileType(String dirPath,
			List<String> fileTypes) {
		
		String localClassFilePath = FileViewer.class.getResource("/").getPath();
		System.out.println(localClassFilePath);
		String contextFilePath = localClassFilePath.substring(1, localClassFilePath.length()-16);
		contextFilePath = contextFilePath.replace("/", "\\");
		System.out.println(contextFilePath);
		
		List<String> filePathList = new ArrayList<String>();
		for (int index = 0; index < fileTypes.size(); index++) {
			fileList = new ArrayList<String>();
			List arrayList = FileViewer.getListFiles(dirPath, fileTypes.get(index), true);
			if (arrayList.isEmpty()) {
				System.out.println("没有符号要求的文件");
			} else {
				String message = "";
				message += "符号要求的文件数:" + arrayList.size();
				System.out.println(message);
				for (Iterator i = arrayList.iterator(); i.hasNext();) {
					String temp = (String) i.next();
					//E:\app\agreeWeb\workagree\fly.info\context\images\fly\信息发布\2012\02\08\06\05\06\26\90852629\1-1\1-1-1\1.jpg
					temp = temp.replace(contextFilePath, "");//把根路径去掉
					System.out.println(temp);
					filePathList.add(temp);
					message += temp + "/r/n";
				}
			}
		}
		for (int replaceIndex = 0; replaceIndex < filePathList.size(); replaceIndex++) {
			filePathList.set(replaceIndex, filePathList.get(replaceIndex).replace("\\", "/"));
		}
		return filePathList;
	}

	public static List<String> fileList = new ArrayList<String>();

	/**
	 * 
	 * @param path
	 *            文件路径
	 * @param suffix
	 *            后缀名
	 * @param isdepth
	 *            是否遍历子目录
	 * @return
	 */
	public static List getListFiles(String path, String suffix, boolean isdepth) {
		File file = new File(path);
		return FileViewer.listFile(file, suffix, isdepth);
	}

	public static List listFile(File f, String suffix, boolean isdepth) {
		// 是目录,同时需要遍历子目录
		if (f.isDirectory() && isdepth == true) {
			File[] t = f.listFiles();
			for (int i = 0; i < t.length; i++) {
				listFile(t[i], suffix, isdepth);
			}
		} else {
			String filePath = f.getAbsolutePath();

			if (suffix != null) {
				int begIndex = filePath.lastIndexOf(".");// 最后一个.(即后缀名前面的.)的索引
				String tempsuffix = "";

				if (begIndex != -1)// 防止是文件但却没有后缀名结束的文件
				{
					tempsuffix = filePath.substring(begIndex + 1, filePath
							.length());
				}

				if (tempsuffix.equals(suffix)) {
					fileList.add(filePath);
				}
			} else {
				// 后缀名为null则为所有文件
				fileList.add(filePath);
			}

		}

		return fileList;
	}

	/**
	 * 方法追加文件:使用FileWriter
	 * 
	 * @param fileName
	 * @param content
	 */
	public static void appendMethod(String fileName, String content) {
		try {
			// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
			FileWriter writer = new FileWriter(fileName, true);
			writer.write(content + "/r/n");
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
//	public static void main(String[] args) {
//		List listResult = getAllFilePathByDir_FilterByFileType(
//				"E:/app/agreeWeb/workagree/fly.info/context/images/fly/信息发布/2012/02/08/02/32/27/227/89857567"
//				, FileUploadUtil.imageFileTypes
//				);
//		int size = listResult.size();
//		System.out.println(size);
//	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值