【问题标题】:Java JTextPane RTF SaveJava JTextPane RTF 保存
【发布时间】:2010-04-27 21:05:58
【问题描述】:

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

【问题讨论】:

  • @ikurtz 你可能会回答你自己的问题(没有声誉),这有助于其他正在寻找答案的人。
  • @stacker:是的,一旦论坛允许我这样做,我会跟进解决问题。我想我要等一天才能提交我的答案。

标签: java swing rtf jtextpane


【解决方案1】:
            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,它就可以工作。

【讨论】:

  • } catch (BadLocationException e){ } 不要忽略异常!
【解决方案2】:

这是针对 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();
}

【讨论】:

    【解决方案3】:

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

        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
    

    【讨论】:

      【解决方案4】:

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

      试试out.close()

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-13
        • 1970-01-01
        • 2018-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多