Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将JTextPane上标文本另存为RTF_Java_Swing_Save_Rtf_Jtextpane - Fatal编程技术网

Java 将JTextPane上标文本另存为RTF

Java 将JTextPane上标文本另存为RTF,java,swing,save,rtf,jtextpane,Java,Swing,Save,Rtf,Jtextpane,如何将JTextPane上标文本保存为RTF 我正在尝试创建类似写字板的文本编辑器。我使用了RTFEditorKit。我可以使用下面的代码添加上标。(例如:X的幂为2) 我将文档保存如下 public void save(String fileName) { BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName)); try { StyledDocument

如何将
JTextPane
上标文本保存为RTF

我正在尝试创建类似写字板的文本编辑器。我使用了
RTFEditorKit
。我可以使用下面的代码添加上标。(例如:X的幂为2)

我将文档保存如下

public void save(String fileName) {
     BufferedOutputStream out = new BufferedOutputStream(new  FileOutputStream(fileName));
   try {
     StyledDocument doc = this.getStyledDocument();
     OutputStream outputStream = new FileOutputStream(fileName);
     this.getEditorKit().write(out, doc, 0, doc.getLength());
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     out.close();
   }
}
它成功地保存了。但当我使用写字板或Microsoft Word上标格式打开它时,它丢失了

使用AdvancedEditorKit,我让它工作了

JTextPane pane = new JTextPane();
AdvancedRTFDocument document = new AdvancedRTFDocument();
AdvancedRTFEditorKit editor = new AdvancedRTFEditorKit();
editor.write("test.rtf", document);

您可以使用alternative
RTFEditorKit


默认设置功能有限,因此许多RTF格式设置功能不受支持。

要更快获得更好的帮助,请发布。谢谢Andre。我会记住的我试过你的高级飞行训练。但它没有拯救任何东西。保存后的文档为空。JTextPane textPane=新的JTextPane();AdvancedRTFEditorKit工具包=新的AdvancedRTFEditorKit();textPane.setEditorKit(工具包);setDocument(新的AdvancedRTFDocument());StyledDocument文档=(StyledDocument)textPane.getDocument();OutputStream out=新文件OutputStream(“output.rtf”);textPane.getEditorKit().write(out,doc,0,doc.getLength());
JTextPane pane = new JTextPane();
AdvancedRTFDocument document = new AdvancedRTFDocument();
AdvancedRTFEditorKit editor = new AdvancedRTFEditorKit();
editor.write("test.rtf", document);