62,615
社区成员
发帖
与我相关
我的任务
分享
int result = jfc.showSaveDialog(yourParentSwingComponent);
if (result == JFileChooser.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
if (!file.getFileName().toLowerCase().endsWith(".txt")) {
file = new File(file.getParent(), file.getName() + ".txt");
}
saveDocInFile(file);
}
package com.ekingstar.cipherapp;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class FileCheckChooser extends JFileChooser {
private static final long serialVersionUID = -7281758066216188169L;
public void approveSelection() {
File file=this.getSelectedFile();
Pattern pattern=Pattern.compile("[:,\",<,>,\\|]");
Matcher matcher=pattern.matcher(file.getName());
if(matcher.find()){
JOptionPane.showMessageDialog(this, "不能输入以下字符:\r\n \" : < > |","错误",JOptionPane.ERROR_MESSAGE);
return ;
}
String modle=this.getApproveButtonText();
if("载入".equals(modle)){
if(!file.exists()){
JOptionPane.showMessageDialog(this, "文件不存在","错误",JOptionPane.ERROR_MESSAGE);
return ;
}
Pattern pattern1=Pattern.compile("^.*\\.key$");
Matcher m= pattern1.matcher(file.getName());
boolean check= m.matches();
if(!check){
JOptionPane.showMessageDialog(this, "请选择密钥文件","错误",JOptionPane.ERROR_MESSAGE);
return ;
}
}else if("生成".equals(modle)){
if(file.exists()){
int select=JOptionPane.showConfirmDialog(this, "文件已经存在,是否需要覆盖?","提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(select==JOptionPane.NO_OPTION){
return;
}
}
}
super.approveSelection();
}
}
this.jfc=new FileCheckChooser();
this.jfc.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter fnef=new FileNameExtensionFilter("Key File","key");
jfc.setFileFilter(fnef);