lucene学习中的工具类封装

    今天学习孔浩lucene的视频第一集,跟着做了一遍,运行时候报错,原因是视频中打开文件夹的那段代码出错,于是就自己封装了个工具类

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 方便使用lucene的工具包
 * @author 
 * @date 2016-8-18 上午11:34:04
 * @company 
 * @version 1.0
 * @copyright copyright (c) 2016
 */
public class LuceneUtils {

	/**
	 * 获取文件夹下的所有满足后缀的文件<br><br>
	 * @author 
	 * @date 2016-8-18 上午11:43:30<br>
	 * @param dirName 要处理的文件夹名
	 * @param suffix 文件后缀,如:txt,avi
	 * @return<br>
	 */
	public static File[] queryTxtFile(String dirName, String suffix){
		File dir = new File(dirName);
		File[] files = dir.listFiles();
		String matchedFileName = "";
		if(files != null){
			for(int i = 0; i < files.length; i++ ){
				String fileName = files[i].getName();
				String[] suffixName = fileName.split("\\.");
				if(suffixName[suffixName.length - 1].matches(suffix)){
					matchedFileName = matchedFileName + fileName +"\\|"; //记录后缀满足的文件名
				}
			}
		}
		if(matchedFileName != ""){
			String[] names = matchedFileName.split("\\|");	//把满足后缀的文件名分割开,创建文件File
			File[] resultFiles = new File[names.length];
			for(int i = 0; i < names.length; i ++){
				resultFiles[i] = new File(dirName + "/" + names[i]);
			}
			return resultFiles;
		}else{
			return null;
		}
	}
	
	
	
}
lucene创建索引的那段代码修改完毕后是

public static void main(String[] args) {
		//1.创建Directory
		//Directory directory = new RAMDirectory();		//建立在内存中
		Directory directory = null;
		//2.创建IndexWriter
		IndexWriterConfig iwc = null;
		IndexWriter writer = null;
		try {
			directory = FSDirectory.open(new File("d:/lucene_test/lucene01"));
			iwc = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
			writer = new IndexWriter(directory, iwc);
			//3.创建Document对象
			Document doc = new Document();
			//4.为Document添加Field
			File[] files = LuceneUtils.queryTxtFile("d:/lucene_test", "txt");
			for(int i = 0; i < files.length; i++){
				doc = new Document();
				doc.add(new Field("content", new FileReader(files[i])));
				doc.add(new Field("filename", files[i].getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
				doc.add(new Field("path", files[i].getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
				writer.addDocument(doc);
			}
			File f = new File("d:/lucene_test");
			
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(writer != null){
				try {
					writer.close();
				} catch (CorruptIndexException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

GBK编码考过来有乱码,下次再整理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值