我有以下的代码块:保存的JTextPane的内容为普通文本文件失败
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);
}
}
}
一旦执行,该代码不会产生任何异常,但我无法找到保存在磁盘上文件的任何地方(我搜索通过本地磁盘与Total Commander)。为什么没有生成文件?我目前正在使用Windows 7旗舰版,并试图保存到登录用户的桌面(因为可能的访问冲突问题......)?
发布您的[SSCCE](http://sscce.org/),演示问题。 – camickr 2013-03-27 05:04:01
你可以尝试调用out.flush();结束之前? – StanislavL 2013-03-27 05:55:22