dirlist.java

来自「java实现的全文搜索引擎」· Java 代码 · 共 91 行

JAVA
91
字号
/**
 * Strategy Pattern.Read the file list from the directory.
 */
package cn.edu.nju.software.ruse;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;

/**
 * @author spring
 */
/**
 * @author spring
 *
 */
public class DirList {

	RUSE ruse;
	
	/**
	 * Hold all the Files' directory according to the path offered.
	 */
	private File[] list;
	
	/**
	 * We can find all the files in the path and we search term in those files.
	 */
	private File path;
	
	private HashSet<File> result = new HashSet<File>();
	
    /**
     * Hint for how to input the parameter.[] is optional parameters.And you can use regular expression to filter.
     */
    private static void usage()
    {
        System.err.println("Usage: RUSE [absolute directory] [regex]");
    }
    
	public DirList() {
		
	}
	
	public DirList(String[] args) {
		if(args.length == 0) {
			path = new File(".");
			list = recurseDirs(path).toArray(new File[0]);
		}else if(args.length == 1){
			path = new File(args[0]);
			list = recurseDirs(path).toArray(new File[0]);
		}else if(args.length == 2) {
			path = new File(args[0]);
			list = path.listFiles(new DirFilter(args[1]));
		}else {
			usage();
		}
	}
	
	/**
	 * Print out the name in  the path.
	 */
	public void printList() {
//System.err.println("Find the following files in the directory!");
		for(File file:list) {
			System.out.println(file.getName());
		}
	}

	public File[] getList() {
		return list;
	}

	public void setList(File[] list) {
		this.list = list;
	}
	
	public HashSet<File> recurseDirs(File startDir) {
		for(File item:startDir.listFiles()) {
			if(item.isDirectory()) {
				result.addAll(recurseDirs(item));
			} else {
				result.add(item);
			}
		}
		return result;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?