您的位置:首页 > 其它

JFileChooser 选择目录,选择保存路径,并对保存文件类型进行更改:

2011-07-14 16:07 387 查看
JFileChooser :

//选择目录
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = chooser.showOpenDialog(null);
String fname = chooser.getName(chooser.getSelectedFile());
if (result == JFileChooser.APPROVE_OPTION) {

String filePath = chooser.getSelectedFile().getPath();
System.out.println(filePath);
}

//保存文件:

JFileChooser fd = new JFileChooser();
HtmlFileFilter hff = new HtmlFileFilter();
fd.addChoosableFileFilter(hff);
fd.showSaveDialog(null);
File f = fd.getSelectedFile();
if (f != null){
System.out.println(f.getAbsolutePath() + ".html");
jTextField2.setText(f.getAbsolutePath() + ".html");
}

过滤器:

class HtmlFileFilter extends FileFilter {
public String getDescription() {
return "*.html(网页文件)";
}

public boolean accept(File file) {
String name = file.getName();
return name.toLowerCase().endsWith(".html");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐