我在我的应用中使用了JFileChooser。我的问题是用户是否在对话框中键入了没有文件扩展名的文件名;该应用程序将允许更多的文件扩展名。如何将文件的文件扩展名作为类型下拉框获取。我使用了fileChooser.getSelectedFile()。getName()或fileChooser.getSelectedFile()。getAbsolutePath()没有获得扩展名。有人会帮我怎么做。
有我的代码:
@SuppressWarnings("static-access")
private String getSavedFileName()
{
String rtnValue="";
JFileChooser fileChooser = new JFileChooser();
String PDFEXTENSION= "pdf";
FileNameExtensionFilter pdfType=new FileNameExtensionFilter("PDF File (."+ PDFEXTENSION + ")", PDFEXTENSION);
fileChooser.addChoosableFileFilter(pdfType);
fileChooser.setFileFilter(pdfType);
//clear "All files" from dropdown filter box
fileChooser.setAcceptAllFileFilterUsed(false);
int returnVal = fileChooser.showSaveDialog(this.splitRightPane);
if (returnVal == fileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
// String extension = file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length());
String fileAbsolutePath=file.getName();
file.getAbsolutePath()
String extension =fileAbsolutePath.substring(fileAbsolutePath.lastIndexOf(".") + 1, fileAbsolutePath.length() );
if ( PDFEXTENSION.equalsIgnoreCase(extension)){
rtnValue=fileAbsolutePath;
} else{
Utility.DisplayWarningMsg("Only PDF File");
}
} else if (returnVal == JFileChooser.CANCEL_OPTION ) {
// Do something else
}
return rtnValue;
}