Java JTextPane RTF保存 [英] Java JTextPane RTF Save

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

问题描述

我有以下代码试图将JTextPane的内容保存为RTF。虽然在以下代码中创建了一个文件,但它是空的!

i have the following code trying to save the contents of a JTextPane as RTF. although a file is created in the following code but it is empty!

关于我做错了什么提示? (像往常一样不要忘记我是初学者!)

any tips regarding what am i doing wrong? (as usual dont forget im a beginner!)

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它可以工作,这就是我真正想要的。求助!

HTMLEditorKit if i use HTMLEditorKit it works and thats what i really wanted. SOLVED!

推荐答案

            if (textPaneHistory.getText().length() > 0){

            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(false);

            int option = chooser.showSaveDialog(ChatGUI.this);

            if (option == JFileChooser.APPROVE_OPTION) {

                StyledDocument doc = (StyledDocument)textPaneHistory.getDocument();

                HTMLEditorKit kit = new HTMLEditorKit();

                BufferedOutputStream out;

                try {
                    out = new BufferedOutputStream(new FileOutputStream(chooser.getSelectedFile().getAbsoluteFile()));

                    kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength());

                } catch (FileNotFoundException e) {

                } catch (IOException e){

                } catch (BadLocationException e){

                }
            }
        }

这是解决方案。如果使用HTMLEditorKit,它可以工作。

here is the solution. it works if HTMLEditorKit is used.

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

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