jFileChooser 文件名 showSaveDialog
// 下面的方法是将一个jTextpane中的文本保存到htm文件中的,jFileChooser.showSaveDialog,
// 下面的方法是将一个jTextpane中的文本保存到htm文件中的,jFileChooser.showSaveDialog,
public void mouseClicked(MouseEvent arg0) {
javax.swing.JFileChooser jfc = new javax.swing.JFileChooser() {
public String paramString() {
return "drhdrhdrh";
}
};
FileFilter filter = new FileFilter() {
public boolean accept(File f) {
return f.isDirectory() || (f.isFile() && (
f.getName().endsWith(".htm")
|| f.getName().endsWith(".HTM")
|| f.getName().endsWith(".html")
|| f.getName().endsWith(".HTML")
));
}
public String getDescription() {
return "保存为HTML文件格式";
}
};
jfc.setFileFilter(filter);
int i = jfc.showSaveDialog(jContentPane);
String fname = null;
if(i == javax.swing.JFileChooser.APPROVE_OPTION) {
File f = jfc.getSelectedFile();
// 注意这里,和下面一句, 如果这里并没有选取中任何的文件,下面的jfc.getName(f)将会返回手输入的文件名
fname = jfc.getName(f);
if(fname != null && fname.trim().length()>0) {
if(fname.endsWith(".htm") || fname.endsWith(".HTM") || fname.endsWith(".html") || fname.endsWith(".HTML"))
;
else {
fname = fname.concat(".htm");
}
}
if(f.isFile())
fname = f.getName();
f = jfc.getCurrentDirectory();
f = new File(f.getPath().concat(File.separator).concat(fname));
if(f.exists()) {
i = javax.swing.JOptionPane.showConfirmDialog(jContentPane, "该文件已经存在,确定要覆盖吗?");
if(i == javax.swing.JOptionPane.YES_OPTION)
;
else
return ;
}
try {
f.createNewFile();
java.io.FileWriter fw = new java.io.FileWriter(f);
fw.write(getJtp_html().getText());
fw.close();
} catch(Exception ex) {
javax.swing.JOptionPane.showMessageDialog(jContentPane, "出错:" + ex.getMessage());
return ;
}
}
}
Labels:
Trackback: http://cwq.iou1314.com/_a154
Trackback: http://cwq.iou1314.com/_a154




















