Java中的RTF到纯文本 [英] RTF to Plain Text in Java

查看:132
本文介绍了Java中的RTF到纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中将RTF字符串转换为纯文本?显而易见的答案是使用Swing的RTFEditorKit,这似乎是互联网上的常见答案。然而,声称返回纯文本的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.

推荐答案

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

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());

那就好了。

这篇关于Java中的RTF到纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆