日期:2014-05-20 浏览次数:20993 次
/**
* 弹出保存对话框,获得保存路径和文件名,用户没有选择则返回null
* @param parentFrame 父窗体
* @return 文件名
*/
public String getPath(Component parentFrame) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("D://"));// 默认打开路径
// 设置过滤文件格式
chooser.setFileFilter(new FileFilter() {
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
if (file.getName().endsWith(".sql")) {
return true;
}
return false;
}
public String getDescription() {
return "*.sql";
}
});
int i = chooser.showSaveDialog(parentFrame);
String fileName = null;
if (JFileChooser.APPROVE_OPTION == i) {
File file = chooser.getSelectedFile();
if (file.exists()) {
int t = JOptionPane.showConfirmDialog(parentFrame,
"该文件已存在,是否覆盖?", "是否覆盖?", JOptionPane.OK_CANCEL_OPTION);
if (t != JOptionPane.OK_OPTION) {
return getPath(parentFrame);
}
}
if (file.toString().endsWith(".sql")) {
fileName = file.toString();
} else {
fileName = file.toString() + ".sql";
}
}
return fileName;
}