2010-04-27 57 views
3

我有下面的代碼嘗試保存爲RTF的JTextPane的內容。儘管在下面的代碼中創建了一個文件,但它是空的!Java 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它的工作原理和多數民衆贊成我真正想要的。解決了!

+0

@ikurtz你可以回答你自己的問題(沒有聲望),這有助於其他人正在尋找答案。 – stacker 2010-04-27 21:39:32

+0

@stacker:是的,一旦論壇允許我這樣做,我會按照解決方案進行解決。我認爲有一天我可以提交我的答案。 – iTEgg 2010-04-27 21:46:54

回答

4
  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。

+0

'} catch(BadLocationException e){ }'不要忽略異常! – 2012-10-29 05:07:45

0

我在代碼中看到的唯一問題是您沒有關閉輸出流(當內容實際寫入磁盤時)。

嘗試out.close()

它應該解決你的問題。

1

這是一個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(); 
} 
1

這就是我在遇到同樣問題時所做的。

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 
+0

不錯的工作,很好的答案。 – 2013-03-12 09:08:49