Java 8:如何将纯UTF-8文本转换为RTF(符号大于127)?
我发现很多使用RTFEditorKit的“如何RTF->纯文本”,但没有一个是“纯文本->RTF”。 我不想手动执行,因为在我的例子中,我们必须将超过128个字符转换为十六进制字符串,以获得正确的RTF文件。我想用一些图书馆来做这件事 我正在尝试使用RTFEditorKit执行此操作:Java 8:如何将纯UTF-8文本转换为RTF(符号大于127)?,java,rtf,richtext,Java,Rtf,Richtext,我发现很多使用RTFEditorKit的“如何RTF->纯文本”,但没有一个是“纯文本->RTF”。 我不想手动执行,因为在我的例子中,我们必须将超过128个字符转换为十六进制字符串,以获得正确的RTF文件。我想用一些图书馆来做这件事 我正在尝试使用RTFEditorKit执行此操作: String orig = "Hello Привет こんにちは"; InputStream is = new ByteArrayInputStream(orig.getBytes(Stan
String orig = "Hello Привет こんにちは";
InputStream is = new ByteArrayInputStream(orig.getBytes(StandardCharsets.UTF_8));
// String tmpStr = is.getText("UTF-8"); // here is correct tmpStr
RTFEditorKit rtfParser = new RTFEditorKit();
javax.swing.text.Document doc = rtfParser.createDefaultDocument();
rtfParser.read(is, doc, 0);
int docLen = doc.getLength(); // !!! here is docLen = 0
OutputStream os = new ByteArrayOutputStream();
rtfParser.write(os, doc, 0, docLen, );
但无法将纯文本读入文档对象。我不确定是否能指出代码中哪里出了问题,但这对我很有用。我将字符串插入
文档中
:
String orig = "Hello Привет こんにちは";
RTFEditorKit rtfParser = new RTFEditorKit();
javax.swing.text.Document doc = rtfParser.createDefaultDocument();
doc.insertString(0, orig, null);
int docLen = doc.getLength();
OutputStream os = Files.newOutputStream(Paths.get("test.rtf"), StandardOpenOption.CREATE);
rtfParser.write(os, doc, 0, docLen);
通过查看这些地方:-您可能只需要使用字符集并使用适当的windows代码页(windows-1250等)-可能根本没有库。JGFMK,是的,我已经读过了,这是我的计划B,手动操作谢谢Alex!“doc.insertString(0,orig,null)”运行良好。