gpt4 book ai didi

java - RTFEditorKit read()/write() 错误?

转载 作者:太空宇宙 更新时间:2023-11-04 13:48:01 25 4
gpt4 key购买 nike

考虑以下代码:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyledDocument;
import javax.swing.text.rtf.RTFEditorKit;


public class TestRTF {
public static void main(String args[]) throws BadLocationException, IOException {
new TestRTF();
}

public TestRTF() throws BadLocationException, IOException {
StyledDocument doc = new DefaultStyledDocument();
JTextPane tp = new JTextPane(doc);
doc.insertString(0, "This is a test", null);
RTFEditorKit kit = new RTFEditorKit();
for(int i=0; i<4; i++) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
kit.write(out, doc, 0, doc.getLength());
String s = out.toString();
System.out.println(s);
System.out.println("-------------------");
doc.remove(0, doc.getLength());
kit.read(new ByteArrayInputStream(s.getBytes()), doc, 0);
}
}
}

我希望这会打印出相同的字符串四次,但是是在从 TextPane 读取的循环中,并将内容替换为读取的内容会导致创建其他段落:

{\rtf1\ansi
{\fonttbl\f0\fnil Monospaced;\f1\fnil Dialog;}
{\colortbl\red0\green0\blue0;\red51\green51\blue51;}

\f1\fs24\i0\b0\cf1 This is a test\par
}

-------------------
{\rtf1\ansi
{\fonttbl\f0\fnil Monospaced;\f1\fnil Dialog;}
{\colortbl\red0\green0\blue0;\red51\green51\blue51;}

\li0\ri0\fi0\f1\fs24\i0\b0\ul0\cf1 This is a test\par
\li0\ri0\fi0\ul0\par
}

-------------------
{\rtf1\ansi
{\fonttbl\f0\fnil Monospaced;\f1\fnil Dialog;}
{\colortbl\red0\green0\blue0;\red51\green51\blue51;}

\li0\ri0\fi0\f1\fs24\i0\b0\ul0\cf1 This is a test\par
\par
\ul0\par
}

-------------------
{\rtf1\ansi
{\fonttbl\f0\fnil Monospaced;\f1\fnil Dialog;}
{\colortbl\red0\green0\blue0;\red51\green51\blue51;}

\li0\ri0\fi0\f1\fs24\i0\b0\ul0\cf1 This is a test\par
\par
\par
\ul0\par
}

-------------------

这是 RTFEditorKit 中的错误,还是我做错了什么?

最佳答案

我想说这是 RTFEditorKIt 中的错误。

实际上是空的DefaultStyledDocument(在套件内部使用)直到最后有一个段落(带有一个\n)。看起来该工具包并不关心该段落,而是每次都会创建新的段落,而不是使用现有的段落。

您可以尝试the alternative RTF Editor Kit它支持更多东西

关于java - RTFEditorKit read()/write() 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30630260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com