文件搜索小工具

package o.o.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/*
 * author:Micky
 * version:1.0.0
 * Date:2017年8月18日 下午11:46:15
 */
public class FileUtils {

	/*
	 * @parameter 指定路径filePath,指定文件名 fileName
	 * 
	 * @return 匹配文件的具体文件路径的列表
	 */
	public List containFileName(String filePath, String fileName) {
		List filePathList = new ArrayList();
		File dir = new File(filePath);
		List files = new ArrayList();
		if (dir.isDirectory()) {
			files = getterAllFiles(dir);
			files.stream().filter(f -> f.getName().contains(fileName))
					.forEach(f -> filePathList.add(f.getAbsolutePath()));
		} else if (dir.isFile() && dir.getName().contains(fileName)) {
			filePathList.add(dir.getAbsolutePath());
		}
		return filePathList;
	}

	/*
	 * 递归得到指定路径下的所有文件
	 */
	public List getterAllFiles(File dir) {
		List files = new ArrayList();
		List childfiles = Arrays.asList(dir.listFiles());
		childfiles.stream().forEach(f -> {
			if (f.isDirectory()) {
				files.addAll(getterAllFiles(f));
			} else {
				files.add(f);
			}
		});
		return files;
	}

	/*
	 * @parameter 指定路径filePath,指定字符串specialString
	 * 
	 * @return 包含特殊字段的文件的具体文件路径的列表
	 */
	public List containSpecialString(String filePath,
			String specialString) {
		List filePathList = new ArrayList();
		File dir = new File(filePath);
		List files = new ArrayList();
		if (dir.isDirectory()) {
			files = getterAllFiles(dir);
			files.stream().filter(f -> isContainSpecialStr(f, specialString))
					.forEach(f -> filePathList.add(f.getAbsolutePath()));
		} else if (dir.isFile() && isContainSpecialStr(dir, specialString)) {
			filePathList.add(dir.getAbsolutePath());
		}
		return filePathList;
	}

	public boolean isContainSpecialStr(File file, String str) {
		String strLine = "";
		boolean flag = false;
		try {
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(new FileInputStream(file)));
			while ((strLine = bufferedReader.readLine()) != null) {
				if (strLine.contains(str)) {
					flag = true;
				}
			}
			if (bufferedReader != null)
				bufferedReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flag;
	}

	/*
	 * @parameter 指定具体文件以及所含特殊字段
	 * 
	 * @return 指定字段所在文件路径及其在文件中的行数以及行具体信息的嵌套map
	 */

	public Map> containSpecialStringReturnMap(
			String filePath, String specialString) {
		Map> filePathMap = new HashMap<>();
		File dir = new File(filePath);
		List files = new ArrayList();
		if (dir.isDirectory()) {
			files = getterAllFiles(dir);
			files.stream()
					.filter(f -> isContainSpecialStr(f, specialString))
					.forEach(
							f -> filePathMap.put(f.getAbsolutePath(),
									containSpecialStrInfo(f, specialString)));
		} else if (dir.isFile() && isContainSpecialStr(dir, specialString)) {
			filePathMap.put(dir.getAbsolutePath(),
					containSpecialStrInfo(dir, specialString));
		}
		return filePathMap;
	}

	/*
	 * @parameter 指定具体文件以及所含特殊字段
	 * 
	 * @return 指定字段在文件中的行数以及行具体信息的map
	 */
	public Map containSpecialStrInfo(File file, String str) {
		Map specialStrInfo = new LinkedHashMap();
		if (file.isDirectory()) {
			return specialStrInfo;
		}

		String strLine = "";
		try {
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(new FileInputStream(file)));
			int row = 1;
			while ((strLine = bufferedReader.readLine()) != null) {
				if (strLine.contains(str)) {
					specialStrInfo.put(row, strLine);
				}
				row++;
			}
			if (bufferedReader != null)
				bufferedReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return specialStrInfo;
	}
}

你可能感兴趣的:(文件小工具)