English 中文(简体)
Java JTextPane RTF Save
原标题:Java JTextPane RTF Save

i 制定了以下法典,试图挽救作为难民工作队的JTextPane的内容。 虽然在以下法典中设立了一个档案,但空洞!

任何关于什么是错过? (与往常一样,遗忘成头脑!)

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

                }
            }
        }

这就是解决办法。 如果是超文本处理员,它就可操作。 使用了工具包。

问题回答

这里是区域工作队而不是超文本的解决办法。

由于难民调查工作队没有标准化,我需要一些额外的标记,如 s和 s,迫使我的手垫适当展示档案。

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("

");
    // set a good Line Space and no Space Before or Space After each paragraph
    rtfContentBuffer.insert(endProlog, "
\sl240");
    rtfContentBuffer.insert(endProlog, "
\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();
}

在我面临同样的问题时,我是如何这样做的。

    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

我在你的守则中看到的唯一问题是,你不再关闭产出流(如果内容实际上写到磁盘上)。

Try out.close()

它应当解决你的问题。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签