RTF to Plain Text in Java

前端 未结 4 1091
南方客
南方客 2020-11-30 15:07

How do you convert an RTF string to plain text in Java? The obvious answer is to use Swing\'s RTFEditorKit, and that seems to be the common answer around the Internet. How

4条回答
  •  再見小時候
    2020-11-30 15:41

    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...");    
         }    
    
        }
    

提交回复
热议问题