Java 中 RTF 到纯文本

发布于 2024-11-04 10:24:40 字数 153 浏览 2 评论 0原文

在 Java 中如何将 RTF 字符串转换为纯文本?显而易见的答案是使用 Swing 的 RTFEditorKit,这似乎是 Internet 上的常见答案。然而,声称返回纯文本的 write 方法实际上并未实现......它是硬编码的,只是在 Java6 中抛出 IOException 。

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. However the write method that claims to return plain text isn't actually implemented... it's hard-coded to just throw an IOException in Java6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听风吹 2024-11-11 10:24:40

我在 Java 6 中使用 Swing 的 RTFEditorKit,如下所示:

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

并且可以正常工作。

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.

你没皮卡萌 2024-11-11 10:24:40

您可能会考虑将 RTF Parser Kit 作为 Swing RTFEditorKit 的轻量级替代品。下面的行显示了从 RTF 文件中提取的纯文本。从输入流读取 RTF 文件,将提取的文本写入输出流。

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

(全面披露:我是 RTF Parser Kit 的作者)

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)

灯角 2024-11-11 10:24:40

这里是解析和解析的完整代码将 RTF 写入纯文本

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

    }

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

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文