1、实现纯文本文档的DocumentHandler接口
2、
public class PlainTextHandler implements DocumentHandler
throws DocumentHandlerException{
String bodyText="";
try{
BufferReader br=new BufferedReader(new InputStreamReader(is));
String line=null;
while ((line==br.readline()!=null)){
bodyText+=line;
}
br.close();
}
catch(IOException e)}{
throw new DocumentHandlerException("cannot read the textdocument",e);
}
if(!bodyText.equals("")){
Documentdoc=new Document();
deoc.add(Field.Unstored("body",bodyText));
return doc;
}
return null;
}
public static void main(String[] args) throwsException{
PlainTextHandler handler=new PlainTextHandler();
Documentdoc=handler.getDocument(new File(args[0]));
System.out.println(doc);
}
}
本文介绍了一个用于处理纯文本文件并将其内容转换为可搜索文档的Java类。该类实现了DocumentHandler接口,并通过读取文件内容到字符串变量中来构建文档。如果文件不为空,则创建一个包含文件正文的Document对象。
1530

被折叠的 条评论
为什么被折叠?



