我现需要遍历一个文件夹内所有的txt文件(该文件夹里有二级、三级文件夹和文件)用递归来写的,如图
//开始遍历路径
File[] files = dir.listFiles();
Analyzer analyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(directory, analyzer, true);
for(int i = 0; i < files.length; i++){
String filePath_index = files[i].getCanonicalPath();//获得索引文件绝对地址
if (files[i].isFile()
&& (files[i].getName().endsWith( ".txt" ) ){ Document document = new Document(); Reader txtReader = new FileReader(files[i]);
document.add(new Field("path",filePath_index,Field.Store.YES,Field.Index.NO));
document.add(new Field("contents",txtReader)); indexWriter.addDocument(document); //断点第二次进入这里(二级文件夹遍历时)就挂掉了 }//是txt文件的情况
}
for(int i = 0; i < files.length; i++){
if (files[i].isDirectory()){
scanFile=files[i].getCanonicalPath();
createIndex();
}// 是文件夹的情况
}
indexWriter.optimize();
indexWriter.close();
return true;但是断点每次进入二级文件夹的indexWriter.addDocument(document);到这步就停掉了查了下,好像是不能多次的建索引(更新索引)
求解决办法
我用的是2.9.4版本
//开始遍历路径
File[] files = dir.listFiles();
Analyzer analyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(directory, analyzer, true);
for(int i = 0; i < files.length; i++){
String filePath_index = files[i].getCanonicalPath();//获得索引文件绝对地址
if (files[i].isFile()
&& (files[i].getName().endsWith( ".txt" ) ){ Document document = new Document(); Reader txtReader = new FileReader(files[i]);
document.add(new Field("path",filePath_index,Field.Store.YES,Field.Index.NO));
document.add(new Field("contents",txtReader)); indexWriter.addDocument(document); //断点第二次进入这里(二级文件夹遍历时)就挂掉了 }//是txt文件的情况
}
for(int i = 0; i < files.length; i++){
if (files[i].isDirectory()){
scanFile=files[i].getCanonicalPath();
createIndex();
}// 是文件夹的情况
}
indexWriter.optimize();
indexWriter.close();
return true;但是断点每次进入二级文件夹的indexWriter.addDocument(document);到这步就停掉了查了下,好像是不能多次的建索引(更新索引)
求解决办法
我用的是2.9.4版本
解决方案 »
- XML的读写
- 有谁知道struts2如何实现https的请求?????????
- JDBC设自动提交为false,但还是自动提交了,不知道什么原因
- 在发送邮件遇到的错误
- 给个完整的类似126信箱页面框架的例子
- 设置数据库字符集的问题
- 绝对是2005年最大的开发难题,不知道大家能不高的定呀(小弟在这里跪求,希望能一位或多位大哥伸出手了拉我一下)
- 虽然没有分,但是仍然要学习!请大虾帮忙了!希望为了学习而不是为了那点分的德高望重的大虾帮忙!
- 高分求程序!!
- 急!!!请各位大侠帮忙,jsp、servlet配置问题!!!
- 《代码大全》是介绍的C还是Java啊?
- 设置 spring 的事务管理,是拦截到 service包,还是拦截到dao包?
throws Exception {
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
if (f.isDirectory()) {
indexDirectory(writer, f);
} else if (f.getName().endsWith(".txt")
||f.getName().endsWith(".html")
||f.getName().endsWith(".shtml")
||f.getName().endsWith(".htm")) {
indexFile(writer, f);
}
}
}可以试下这种写法
而且也没解决索引不能重复写(更新)的问题继续坐等
更新索引或新建索引是在indexwriter中定义的
第二次循环到
indexWriter.addDocument(document);依然不行不知道咋办了
我之前没搞清楚writer