Java JTextPane RTF 保存

发布于 2024-08-31 04:58:38 字数 935 浏览 3 评论 0原文

我有以下代码尝试将 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 它可以工作,这就是我真正想要的。解决了!

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){
}
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聊慰 2024-09-07 04:58:38
            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 它就可以工作。

            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){

                }
            }
        }

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

朱染 2024-09-07 04:58:38

这是 RTF 而不是 HTML 的解决方案。

由于 RTF 没有标准化,我需要一些额外的标签,如 \sb 和 \sa 来强制我的写字板正确显示文件。

protected void exportToRtf() throws IOException, BadLocationException {
    final StringWriter out = new StringWriter();
    Document doc = textPane.getDocument();
    RTFEditorKit kit = new RTFEditorKit();
    kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength());
    out.close();

    String rtfContent = out.toString();
    {
    // replace "Monospaced" by a well-known monospace font
    rtfContent = rtfContent.replaceAll("Monospaced", "Courier New");
    final StringBuffer rtfContentBuffer = new StringBuffer(rtfContent);
    final int endProlog = rtfContentBuffer.indexOf("\n\n");
    // set a good Line Space and no Space Before or Space After each paragraph
    rtfContentBuffer.insert(endProlog, "\n\\sl240");
    rtfContentBuffer.insert(endProlog, "\n\\sb0\\sa0");
    rtfContent = rtfContentBuffer.toString();
    }

    final File file = new File("c:\\temp\\test.rtf");
    final FileOutputStream fos = new FileOutputStream(file);
    fos.write(rtfContent.toString().getBytes());
    fos.close();
}

Here is a solution for RTF and not HTML.

As RTF is not standardized, I needed some additional tags like \sb and \sa to force my Wordpad to display the file properly.

protected void exportToRtf() throws IOException, BadLocationException {
    final StringWriter out = new StringWriter();
    Document doc = textPane.getDocument();
    RTFEditorKit kit = new RTFEditorKit();
    kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength());
    out.close();

    String rtfContent = out.toString();
    {
    // replace "Monospaced" by a well-known monospace font
    rtfContent = rtfContent.replaceAll("Monospaced", "Courier New");
    final StringBuffer rtfContentBuffer = new StringBuffer(rtfContent);
    final int endProlog = rtfContentBuffer.indexOf("\n\n");
    // set a good Line Space and no Space Before or Space After each paragraph
    rtfContentBuffer.insert(endProlog, "\n\\sl240");
    rtfContentBuffer.insert(endProlog, "\n\\sb0\\sa0");
    rtfContent = rtfContentBuffer.toString();
    }

    final File file = new File("c:\\temp\\test.rtf");
    final FileOutputStream fos = new FileOutputStream(file);
    fos.write(rtfContent.toString().getBytes());
    fos.close();
}
子栖 2024-09-07 04:58:38

当我遇到同样的问题时,我就是这样做的。

    public void actionPerformed(ActionEvent e) {

        text = textPane.getText();
        JFileChooser saveFile = new JFileChooser();
        int option = saveFile.showSaveDialog(null);
        saveFile.setDialogTitle("Save the file...");

        if (option == JFileChooser.APPROVE_OPTION) {

            File file = saveFile.getSelectedFile();
            if (!file.exists()) {

                try {
                    BufferedWriter writer = new BufferedWriter(
                            new FileWriter(file.getAbsolutePath() + ".rtf"));
                    writer.write(text);
                    writer.close();

                } catch (IOException ex) {

                    ex.printStackTrace();
                    System.out.println(ex.getMessage());
                    JOptionPane.showMessageDialog(null,
                            "Failed to save the file");
                }

            }

            else if (file.exists()) {

                int confirm = JOptionPane.showConfirmDialog(null,
                        "File exists do you want to save anyway?");
                if (confirm == 0) {

                    try {
                        BufferedWriter writer = new BufferedWriter(
                                new FileWriter(file.getAbsolutePath()
                                        + ".rtf"));
                        writer.write(text);
                        writer.close();

                    } catch (IOException ex) {

                        ex.printStackTrace();
                        System.out.println(ex.getMessage());
                        JOptionPane.showMessageDialog(null,
                                "Failed to save the file");
                    }

                }

                else if (confirm == 1) {

                    JOptionPane.showMessageDialog(null,
                            "The file was not saved.");

                }

            }

        }

        if (option == JFileChooser.CANCEL_OPTION) {

            saveFile.setVisible(false);

        }

    }// End of method

This is how I did when I faced the same problem.

    public void actionPerformed(ActionEvent e) {

        text = textPane.getText();
        JFileChooser saveFile = new JFileChooser();
        int option = saveFile.showSaveDialog(null);
        saveFile.setDialogTitle("Save the file...");

        if (option == JFileChooser.APPROVE_OPTION) {

            File file = saveFile.getSelectedFile();
            if (!file.exists()) {

                try {
                    BufferedWriter writer = new BufferedWriter(
                            new FileWriter(file.getAbsolutePath() + ".rtf"));
                    writer.write(text);
                    writer.close();

                } catch (IOException ex) {

                    ex.printStackTrace();
                    System.out.println(ex.getMessage());
                    JOptionPane.showMessageDialog(null,
                            "Failed to save the file");
                }

            }

            else if (file.exists()) {

                int confirm = JOptionPane.showConfirmDialog(null,
                        "File exists do you want to save anyway?");
                if (confirm == 0) {

                    try {
                        BufferedWriter writer = new BufferedWriter(
                                new FileWriter(file.getAbsolutePath()
                                        + ".rtf"));
                        writer.write(text);
                        writer.close();

                    } catch (IOException ex) {

                        ex.printStackTrace();
                        System.out.println(ex.getMessage());
                        JOptionPane.showMessageDialog(null,
                                "Failed to save the file");
                    }

                }

                else if (confirm == 1) {

                    JOptionPane.showMessageDialog(null,
                            "The file was not saved.");

                }

            }

        }

        if (option == JFileChooser.CANCEL_OPTION) {

            saveFile.setVisible(false);

        }

    }// End of method
夜无邪 2024-09-07 04:58:38

我在您的代码中看到的唯一问题是您没有关闭输出流(当内容实际写入磁盘时)。

尝试out.close()

它应该可以解决你的问题。

The only problem I've seen in your code is that you're not closing the output stream (when content is actually written to disk).

Try out.close().

It should solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文