//弹出文件选择框
JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("请选择要导出目录");
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
// jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = jfc.showOpenDialog(this);
File file = null;
if(JFileChooser.APPROVE_OPTION == result) {
file = jfc.getSelectedFile();
if(!file.isDirectory()) {
JOptionPane.showMessageDialog(null, "你选择的目录不存在");
return ;
}
String path = file.getAbsolutePath();
file = new File(path + "\\"+ "www_NoExist.xls");
if(!file.isFile()) {
if(!file.isFile()) {
JOptionPane.showMessageDialog(null, "文件不存在");
return ;
}
}
} else {
return ;
}
小计一下:SWING中文件和目录选择器是如何生成的。
JFileChooser.DIRECTORIES_ONLY //只有目录
JFileChooser.FILES_ONLY //只有文件
JFileChooser.FILES_AND_DIRECTORIES //全部(默认值)