javabuiltinrtfhandler.java

来自「LuceneInAction配套源码,LuceneInAction是对lucen」· Java 代码 · 共 53 行

JAVA
53
字号
package lia.handlingtypes.rtf;import lia.handlingtypes.framework.DocumentHandler;import lia.handlingtypes.framework.DocumentHandlerException;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import javax.swing.text.DefaultStyledDocument;import javax.swing.text.rtf.RTFEditorKit;import javax.swing.text.BadLocationException;public class JavaBuiltInRTFHandler implements DocumentHandler {  public Document getDocument(InputStream is)    throws DocumentHandlerException {    String bodyText = null;    DefaultStyledDocument styledDoc = new DefaultStyledDocument();    try {      new RTFEditorKit().read(is, styledDoc, 0);      bodyText = styledDoc.getText(0, styledDoc.getLength());    }    catch (IOException e) {      throw new DocumentHandlerException(        "Cannot extract text from a RTF document", e);    }    catch (BadLocationException e) {      throw new DocumentHandlerException(        "Cannot extract text from a RTF document", e);    }    if (bodyText != null) {      Document doc = new Document();      doc.add(Field.UnStored("body", bodyText));      return doc;    }    return null;  }  public static void main(String[] args) throws Exception {    JavaBuiltInRTFHandler handler = new JavaBuiltInRTFHandler();    Document doc = handler.getDocument(      new FileInputStream(new File(args[0])));    System.out.println(doc);  }}

⌨️ 快捷键说明

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