java 文件统计 工具类

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

/**
 * list the files in a folder,and statistic files by type
 * 
 * @author eric
 * @date 2011-2-14 下午05:12:18
 */
public class FileStatistic {
	/**
	 * list & statistic files in folder
	 * 
	 * @param folderPath path of folder to statistic
	 * @param outputPath path of result file
	 * @throws IOException
	 */
	public static void statisticFile(String folderPath, String outputPath) throws IOException {
		File folder = new File(folderPath); // folder to statistic
		File outputFile = new File(outputPath); // output file
		if (!outputFile.exists()) { // create output file if not exists
			outputFile.createNewFile();
		}
		FileWriter fw = new FileWriter(outputFile);

		if (!folder.exists()) { // folder not exists
			fw.write("folder not exists!");
			fw.flush();
			return;
		} else {
			fw.write("'" + folderPath + "' statistic result - " + new Date() + "\n");
		}

		List<String> filenameList = new ArrayList<String>();
		TreeMap<String, Integer> typecountMap = new TreeMap<String, Integer>();

		List<File> fList = getFilesRecursive(folder);
		String keyFolder = "folder"; // key for folder
		typecountMap.put(keyFolder, 0);
		for (int i = 0; i < fList.size(); i++) {
			File _file = fList.get(i);
			String _filename = _file.getName();
			filenameList.add(_filename); // store filename

			if (_file.isDirectory()) { // folder
				typecountMap.put(keyFolder, typecountMap.get(keyFolder) + 1);
			} else { // file
				String suf = getSuf(_filename);
				// statistic file type
				if (typecountMap.get(suf) == null) {
					typecountMap.put(suf, 1);
				} else {
					typecountMap.put(suf, typecountMap.get(suf) + 1);
				}
			}
		}

		fw.append("\nfile list:\n");
		for (Iterator<String> it = filenameList.iterator(); it.hasNext();) {
			fw.append("\t" + it.next() + "\n");
		}

		fw.append("\nfile type static:\n");
		for (Iterator<String> it = typecountMap.keySet().iterator(); it.hasNext();) {
			String _key = (String) it.next();
			fw.append("\t" + _key + "\t\t" + typecountMap.get(_key) + "\n");
		}

		fw.flush();
		fw.close();
	}

	/**
	 * get suffix of filename
	 * 
	 * @param filename
	 * @return
	 */
	public static String getSuf(String filename) {
		int lastIndex = filename.lastIndexOf(".");
		if (lastIndex < 0) {
			return "";
		} else {
			return filename.substring(lastIndex);
		}
	}

	/**
	 * 获得 某目录下所有的子文件列表
	 * 
	 * @param root
	 * @return
	 */
	private static List<File> getFilesRecursive(File root) {
		List<File> fileList = new ArrayList<File>();
		List<File> tmpFolderList = Arrays.asList(root);
		while (!tmpFolderList.isEmpty()) {
			tmpFolderList = getFilesRecursiveInner(fileList, tmpFolderList);
		}
		return fileList;
	}

	/**
	 * 获得 将指定目录list 下 直接 子目录/文件 放入 list,并将 直接子目录 放入 另一个 list 返回
	 * 
	 * @param fileList
	 * @param folderList
	 * @return
	 */
	private static List<File> getFilesRecursiveInner(List<File> fileList, List<File> folderList) {
		List<File> subFolderList = new ArrayList<File>();
		for (Iterator<File> it = folderList.iterator(); it.hasNext();) {
			File _folder = (File) it.next();
			File[] _files = _folder.listFiles();
			if (_files != null) {
				for (int i = 0; i < _files.length; i++) {
					File _file = _files[i];
					fileList.add(_file);
					if (_file.isDirectory()) {
						subFolderList.add(_file);
					}
				}
			}
		}
		return subFolderList;
	}

	public static void main(String[] args) throws IOException {
		statisticFile("/usr/lib", "/home/eric/filestatistic.txt");
	}
}
 

你可能感兴趣的:(java)