I have following chunk of code:
private void saveAs()
{
CDocument currentDocument=this.panelMain().openedDocuments().get(this.panelMain().openedDocuments().size()-1);
StyledDocument contents=currentDocument.getStyledDocument();
DefaultEditorKit kit=new DefaultEditorKit();
JFileChooser chooserSaveAs=new JFileChooser();
chooserSaveAs.setDialogTitle("Save as ...");
if(chooserSaveAs.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
{
String strNewFilename=chooserSaveAs.getSelectedFile().getName();
BufferedOutputStream out;
try
{
out=new BufferedOutputStream(new FileOutputStream(strNewFilename));
kit.write(out,
contents,
contents.getStartPosition().getOffset(),
contents.getLength());
out.close();
}
catch(IOException | BadLocationException ex)
{
Logger.getLogger(CFrameMain.class.getName()).log(Level.SEVERE,
null,
ex);
}
}
}
Once executed, this codes DOES NOT generate any exceptions, but I cannot find saved file on disk anywhere (I've searched through local disk with Total Commander). Why there is no file generated? I am currently working on Windows 7 Ultimate and I tried to save in to logged user's desktop (because of possible access violations problems ...)?
No, but I've managed to fix it. I used @Tom's approach and it works! Here is the code chunk:
try
{
out=new BufferedOutputStream(new FileOutputStream(file));
kit.write(out,
contents,
contents.getStartPosition().getOffset(),
contents.getLength());
out.close();
}
catch(IOException | BadLocationException ex)
{
bError=true;
Logger.getLogger(CFrameMain.class.getName()).log(Level.SEVERE,
null,
ex);
}
finally
{
if(bError!=true)
{
currentDocument.setFilename(chooserSaveAs.getSelectedFile().getAbsolutePath());
this.toolBarFileSwitcher().listOpenedFiles().model().set(this.toolBarFileSwitcher().listOpenedFiles().getSelectedIndex(),
currentDocument.filename());
this.toolBarFileSwitcher().updateUI();
}
}