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

📄 abstractsavorandloader.java

📁 一个用于搜索本地文件内容的小型搜索引擎
💻 JAVA
字号:
package invertedList;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;
import java.util.Set;

/**
 * @author Administrator
 *	an abstract class to handle the index on the disk. 
 *	including saving and loading procedure
 */
public abstract class AbstractSavorAndLoader {
	/**
	 * a copy instance of the allInvertedList in class InvertedListManager
	 */
	protected Map<String,Set<String>> allInvertedList;
	/**
	 * a copy instance of the fileList in class InvertedListManager
	 */
	protected Set<File> fileList;
	
	/**
	 * @param dir
	 * save the indexes and the filelist to a specific directory
	 */
	public void saveIndex(String dir){
		File directory = new File(dir);
		if(!directory.exists()){
			directory.mkdir();
		}
		System.out.println("Saving index files...");
		
		saveInvertedList(dir);
		saveFileList(dir);
		System.out.println();
	}
	
	/**
	 * @param dir
	 * @param force
	 * load the indexes and fileList form a specific directory
	 * if the force parameter is true, then it will loaded from disk no matter if the indexes
	 * is already in the ram.
	 */
	public void loadIndex(String dir,boolean force){
		/*if(force){
			loadFileList(dir);
			//loadVagueIndex(dir);
			loadInvertedList(dir);
			System.out.println("Index Reloading Completed");
		}
		else{
			if(fileList.isEmpty()){*/
				loadFileList(dir);
			//}
			//if(allInvertedList.isEmpty()){
				loadInvertedList(dir);
			//}
			System.out.println("Index Loading Completed");
			System.out.println();
		//}
	}
	
	/**
	 * @param dir
	 * abstract method to save InvertedList, should be implemented by subclasses
	 */
	protected abstract void saveInvertedList(String dir);
	
	/**
	 * @param dir
	 * abstract method to load InvertedList, should be implemented by subclasses
	 */
	protected abstract void loadInvertedList(String dir);
	
	/**
	 * @param dir
	 * abstract method to save fileList, should be implemented by subclasses
	 */
	private void saveFileList(String dir){
		try {			
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(dir + "/fileIndex.xbq")));
			oos.writeObject(fileList);
			oos.flush();
			oos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("Saving Failed, file can not be created.");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Saving Failed, file can not be written.");
		}
	}
	
	/**
	 * @param dir
	 * abstract method to load fileList, should be implemented by subclasses
	 */
	@SuppressWarnings("unchecked")
	protected void loadFileList(String dir) {
		try {
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(dir + "/fileIndex.xbq")));
			fileList = (Set<File>)ois.readObject();
			
			ois.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("Loading Failed. File can not be found.");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Loading Failed. File can not be read.");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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