JAVA搜索文件内容~

我这初学者第一次写博客~ 把我刚写的一段小代码粘贴上来吧
package local;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SimpleLocalDriveSearch {
	private static List result = new ArrayList();
	private static List over = new ArrayList();
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		String ext = new BufferedReader(new InputStreamReader(System.in)).readLine();
		SimpleLocalDriveSearch slds = new SimpleLocalDriveSearch();
		File[] roots = File.listRoots();
		for(int i=0;i<roots.length;i++){
			List found = slds.search(roots[i], ext);
			if(found.size()>0){
				for(Iterator iter = found.iterator();iter.hasNext();){
					System.out.println(iter.next());
					
				}
			}else{
				System.out.println("No match on"+roots[i]);
			}
			if(over.size()>0){
				for(Iterator it = over.iterator();it.hasNext();){
					System.out.println(it.next());
				}
			}
		}		
	}
	/**
	 * 
	 * @return
	 */
	public List search(File startDir,String matchingExtension){
		result.clear();
		resursiveSearch(startDir,matchingExtension);
		return result;
	}
	/**
	 * 
	 * @param rootDir
	 * @param suffix
	 */
	public void resursiveSearch(File rootDir,String suffix){
		File[] files = rootDir.listFiles();
		if(files!=null){
			for(int i=0;i<files.length;i++){
				if(files[i].isDirectory()){
					//System.out.println(files[i]+"............是目录");
					searchDirectory(files[i], suffix);
				}
				if(files[i].isFile() && files[i].getName().contains(suffix)){
						//result.add(files[i]);
						searchFile(files[i], "日志");
				}
			}
		}
	}
	/**
	 * 
	 * @param Dir
	 * @param word
	 * @return
	 */
	public List searchDirectory(File Dir,String word){
		resursiveSearch(Dir, word);
		return result;
	}
	/**
	 * 
	 * @param file
	 * @param w
	 */
	public void searchFile(File file,String w){
		String line = null;
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			while((line = br.readLine())!=null){
				if(line.contains(w)){
					over.add(file);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

有点乱... 能实现搜索文件里的 内容...

你可能感兴趣的:(java,ext)