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

📄 mydocument.java

📁 ajax lucene 部分源代码 HTMLParser.java MuiltiSearchTest.java
💻 JAVA
字号:
package performance.document;

import java.io.*;
import java.util.StringTokenizer;

import org.apache.lucene.document.*;

public class MyDocument {
	
	/**
	 * 将文本文档转成Lucene的Document格式
	 * @param file
	 * @return document that represents a file
	 * @throws Exception
	 */
	public static Document getDocument(File file) throws IOException {
		Document doc = new Document();

		/**
		 * 为文件路径构建一个字段
		 */
		doc.add(Field.Text("path", file.getPath()));

		/**
		 * 为文件名构建一个字段
		 */
		doc.add(Field.Keyword("title", getFileName(file)));

		/**
		 * 为文件内容构建一个字段
		 */
		FileInputStream is = new FileInputStream(file);
		Reader reader = new BufferedReader(new InputStreamReader(is));
		doc.add(Field.Text("contents", reader));

		/**
		 * 为文件的最后修改时间构建一个字段
		 */
		doc.add(Field.Keyword("modified", DateField.timeToString(file
				.lastModified())));

		return doc;
	}

	/**
	 * 
	 * @param file
	 * @return
	 */
	private static String getFileName(File file) {
		String path = file.getPath();
		StringTokenizer st = new StringTokenizer(path, File.separator);
		String token = "";
		while (st.hasMoreTokens()) {
			token = st.nextToken();
		}

		if (token != null) {
			token = token.substring(0, token.indexOf(".txt"));
		}
		return token;
	}
}

⌨️ 快捷键说明

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