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

📄 cdscanner.java

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

import java.io.File;
import java.io.IOException;
import java.util.Date;
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 CDScanner implements Scanner, Runnable{
	
	private int recordID;
	private CD belongCD;
	private String rootID;
	private IndexWriter indexWriter;
	private String scanPath;
	private final File CD_INDEX_DIR = new File("cdindex");
	
	private Config config;
	
	/**
	 * 默认构造函数
	 */
	public CDScanner(){
		recordID = 0;
		rootID= "" + new Date().getTime();
		if(!CD_INDEX_DIR.exists())
			CD_INDEX_DIR.mkdirs();
		scanPath = "";
		config = CDManager.action.getConfig();
	}
	
	/**
	 * 构造函数
	 * @param cd 要遍历的光盘
	 */
	public CDScanner(CD cd) {
		recordID = 0;
		this.rootID = cd.getID();
		belongCD = cd;
		if(!CD_INDEX_DIR.exists())
			CD_INDEX_DIR.mkdirs();
		scanPath = "";
		config = CDManager.action.getConfig();
	}
	
	/**
	 * 构造函数
	 * @param cd 要遍历的光盘
	 * @param path 要扫描的路径
	 */
	public CDScanner(CD cd, String path) {
		recordID = 0;
		this.rootID = cd.getID();
		belongCD = cd;
		if(!CD_INDEX_DIR.exists())
			CD_INDEX_DIR.mkdirs();
		scanPath = path;
		config = CDManager.action.getConfig();
	}
	
	/**
	 * 遍历一张光盘的完整动作
	 * @param path 光盘的路径
	 * @return true 如果遍历完成
	 */
	public boolean scan(String path){
		boolean isIndexExists = false;
		if(IndexReader.indexExists(CD_INDEX_DIR))
			isIndexExists = true;
		try{
			File f = new File(CD_INDEX_DIR + File.separator + "write.lock");
			if(f.exists())
				f.delete();
			indexWriter = new IndexWriter(CD_INDEX_DIR, new StandardAnalyzer(), !isIndexExists, IndexWriter.MaxFieldLength.LIMITED);
			indexWriter.setMaxBufferedDocs(20);
		}
		catch(IOException ie){
			ie.printStackTrace();	
		}
		
		try {
			doScan(path, 0);
			indexWriter.optimize();
			indexWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	/**
	 * 遍历一张光盘,存储其数据索引到数据库.该函数将被scan()递归调用
	 * @param path 光盘的路径
	 * @param parentID 该节点父亲的ID
	 * @return true 如果遍历完成
	 */
	private boolean doScan(String path, int parentID){
		
	    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()) {
	        	belongCD.folderIncrease();
	        	recordID++;
	        	doc = toDocument(files[i], recordID, parentID);
	        	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(), recordID);  
	        }
	        else {  
	        	belongCD.fileIncrease();
	        	recordID++;
	        	config.setScanningPath(files[i].getAbsolutePath());
	        	doc = toDocument(files[i], recordID, parentID);
	        	doc.add(new Field("isLeaf", "t", Field.Store.YES, Field.Index.NO));
	        	try{
	        		indexWriter.addDocument(doc);
	        	} catch(IOException e){
	        		e.printStackTrace();
	        	}
	        }
	    }    
		return true;
	}
	
	/**
	 * 将遍历的文件转化成Document作为索引存储起来
	 * @param file 当前遍历的文件
	 * @param ID 文件的ID
	 * @param parentID 该文件父亲的ID
	 * @return 转化成的Document
	 */
	private Document toDocument(File file, int ID, int parentID){
		Document doc = new Document();
		doc.add(new Field("name", file.getName(), Field.Store.YES, Field.Index.ANALYZED));
		doc.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NO));
		doc.add(new Field("ID", ""+ID, Field.Store.YES, Field.Index.NOT_ANALYZED));
		doc.add(new Field("parentID", ""+parentID, Field.Store.YES, Field.Index.NOT_ANALYZED));
		doc.add(new Field("rootID", rootID, Field.Store.YES, Field.Index.NOT_ANALYZED));
		doc.add(new Field("date", ""+file.lastModified(), Field.Store.YES, Field.Index.NO));
		doc.add(new Field("size", ""+file.length(), Field.Store.YES, Field.Index.NO));
		return doc;
	}

	@Override
	/**
	 * 实现Runnable接口中定义的方法
	 */
	public void run() {
		scan(scanPath);
		Database.getDatabase().store(belongCD);
		Database.getDatabase().store(new Log("创建光盘记录完成,光盘名为:"+ belongCD.getName()));
		IndexFactory.closeCDIndexReader();
		IndexFactory.closeCDIndexSearcher();
		config.setScanning(false);
	}

}
	

⌨️ 快捷键说明

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