我有以下代码试图将 JTextPane 的内容保存为 RTF。虽然在下面的代码中创建了一个文件,但它是空的!
关于我做错了什么的任何提示?(像往常一样不要忘记我是初学者!)
if (option == JFileChooser.APPROVE_OPTION) {
////////////////////////////////////////////////////////////////////////
//System.out.println(chooser.getSelectedFile().getName());
//System.out.println(chooser.getSelectedFile().getAbsoluteFile());
///////////////////////////////////////////////////////////////////////////
StyledDocument doc = (StyledDocument)textPaneHistory.getDocument();
RTFEditorKit kit = new RTFEditorKit();
BufferedOutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream(chooser.getSelectedFile().getName()));
kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength());
} catch (FileNotFoundException e) {
} catch (IOException e){
} catch (BadLocationException e){
}
}
编辑: HTMLEditorKit 如果我使用 HTMLEditorKit 它可以工作,这就是我真正想要的。解决了!