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!
here is the solution. it works if HTMLEditorKit is used.
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.
This is how I did when I faced the same problem.
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.