⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 discscanner.java

📁 CD Manager光盘资料管家婆源代码
💻 JAVA
字号:
package com.galaxyworkstation.model;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;

import com.galaxyworkstation.control.CDManager;


/**
 * 该类模拟了对硬盘的遍历,采用递归的方式,并实现了Scanner接口和Runnable接口
 * @author 李奕
 * @version 1.0
 * @see Scanner
 */
public class DiscScanner implements Scanner, Runnable{

	public static int WHOLE_SCAN = 0;
	public static int DIRECTORY_SCAN = 1;	
	private IndexWriter indexWriter;
	private final File DISC_INDEX_DIR = new File("discindex");
	private String scanPath;
	private int type;
	
	private Config config;
	
	/**
	 * 默认构造函数,实现全盘扫描
	 */
	public DiscScanner(){
		if(!DISC_INDEX_DIR.exists())
			DISC_INDEX_DIR.mkdirs();
		this.type = WHOLE_SCAN;
		this.scanPath = "";
		config = CDManager.action.getConfig();
	}
	
	/**
	 * 构造函数,实现指定路径扫描
	 * @param path 指定扫描的路径
	 */
	public DiscScanner(String path){
		if(!DISC_INDEX_DIR.exists())
			DISC_INDEX_DIR.mkdirs();
		this.scanPath = path;
		this.type = DIRECTORY_SCAN;
		config = CDManager.action.getConfig();
	}
	
	/**
	 * 遍历整个硬盘,存储其数据索引到数据库
	 * @return true 如果遍历完成
	 */
	public boolean scanWholeDisc(){
		try {
			File f = new File(DISC_INDEX_DIR + File.separator + "write.lock");
			if(f.exists())
				f.delete();
			
			indexWriter = new IndexWriter(DISC_INDEX_DIR,  new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
			indexWriter.setMaxBufferedDocs(100);
			File[] roots = File.listRoots();
			for(int i=0; i<roots.length; ++i){
				if(roots[i].getFreeSpace() == 0)
					continue; 
				doScan(roots[i].getAbsolutePath());
			}
			indexWriter.optimize();
			indexWriter.close();
		} catch (IOException e) {
        	e.printStackTrace();
		}	
		return true;
	}
	
	/**
	 * 遍历硬盘的一个目录的完整操作
	 * @param path 目录的路径
	 * @return true 如果遍历完成
	 */
	public boolean scan(String path){
		boolean isIndexExists = false;
		if(IndexReader.indexExists(DISC_INDEX_DIR))
			isIndexExists = true;
		try {
			File f = new File(DISC_INDEX_DIR + File.separator + "write.lock");
			if(f.exists())
				f.delete();
			
			indexWriter = new IndexWriter(DISC_INDEX_DIR,  new StandardAnalyzer(), !isIndexExists, IndexWriter.MaxFieldLength.LIMITED);
			doScan(path);
			indexWriter.optimize();
			indexWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	/**
	 * 遍历硬盘的某个路径,存储其数据索引到数据库.该函数将被scan()递归调用
	 * @param path 待扫描目录的路径
	 * @return true 如果遍历完成
	 */
	private boolean doScan(String path){
		
	    File dir = new File(path); 
	    File[] files = dir.listFiles();
	    Document doc;
	    
	    if (files == null)  
	        return false; 
	    for (int i = 0; i < files.length; i++) {  
	        if (files[i].isDirectory()) {
	        	doc = toDocument(files[i]);
	        	doc.add(new Field("isLeaf", "f", Field.Store.YES, Field.Index.NO));
	        	try{
	        		indexWriter.addDocument(doc);
	        	} catch(IOException e){
	        		e.printStackTrace();
	        	}
	            doScan(files[i].getAbsolutePath());  
	        }
	        else {  
	        	config.setScanningPath(files[i].getAbsolutePath());
	        	doc = toDocument(files[i]);
	        	doc.add(new Field("isLeaf", "t", Field.Store.YES, Field.Index.NO));
	        	try{
	        		indexWriter.addDocument(doc);
	        	} catch(IOException e){
	        		e.printStackTrace();
	        	}
	        }
	    }    
		return true;
	}
	
	/**
	 * 将File类型的对象转化成Document文档类型的对象
	 * @param file 待转化的File类型的对象
	 * @return 转化后的Document文档类型的对象
	 */
	private Document toDocument(File file){
		Document doc = new Document();
		doc.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NO));
		doc.add(new Field("name", file.getName(), Field.Store.YES, Field.Index.ANALYZED));
		doc.add(new Field("size", ""+file.length(), Field.Store.YES, Field.Index.NO));
		return doc;
	}

	@Override
	/**
	 * 实现Runnable接口中定义的方法
	 */
	public void run() {
		if(type == WHOLE_SCAN)
			scanWholeDisc();
		else scan(scanPath);
	
		IndexFactory.closeDiscIndexReader();
		IndexFactory.closeDiscIndexSearcher();
		config.setScanning(false);
	}

}

⌨️ 快捷键说明

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