RTF to Plain Text in Java

让人想犯罪 __ 提交于 2019-11-27 05:34:49

I use Swing's RTFEditorKit in Java 6 like this:

RTFEditorKit rtfParser = new RTFEditorKit();
Document document = rtfParser.createDefaultDocument();
rtfParser.read(new ByteArrayInputStream(rtfBytes), document, 0);
String text = document.getText(0, document.getLength());

and thats working.

You might consider RTF Parser Kit as a lightweight alternative to the Swing RTFEditorKit. The line below shows plain text extraction from an RTF file. The RTF file is read from the input stream, the extracted text is written to the output stream.

new StreamTextConverter().convert(new RtfStreamSource(inputStream), outputStream, "UTF-8");

(full disclosure: I'm the author of RTF Parser Kit)

Here is the full code to parse & write RTF as a plain text

    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Document;
    import javax.swing.text.rtf.RTFEditorKit;

    public class rtfToJson {
    public static void main(String[] args)throws IOException, BadLocationException {
    // TODO Auto-generated method stub
    RTFEditorKit rtf = new RTFEditorKit();
    Document doc = rtf.createDefaultDocument();

    FileInputStream fis = new FileInputStream("C:\\SampleINCData.rtf");
    InputStreamReader i =new InputStreamReader(fis,"UTF-8");
    rtf.read(i,doc,0);
   // System.out.println(doc.getText(0,doc.getLength()));
    String doc1 = doc.getText(0,doc.getLength());


    try{    
           FileWriter fw=new FileWriter("B:\\Sample INC Data.txt");    
           fw.write(doc1);    
           fw.close();    
          }catch(Exception e)
    {
              System.out.println(e);
              }    
          System.out.println("Success...");    
     }    

    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!