我正在制作一个桌面应用程序,它有一个 JFileChooser(ShowSaveDialog) 函数。当我尝试保存示例文本文件时,程序没有得到我选择的扩展文件。我正在尝试使用 if else 或 switch 语句和如果选择 pdf、word 或 txt 扩展名作为文件扩展名,我无法弄清楚我将使用什么命令来获取条件的字符串/Int 值...
public class Save {
static boolean flag = false;
public static void main(String[] args) throws IOException, SQLException {
JFileChooser saveFile = new JFileChooser();
saveFile.setDialogTitle("Save as");
FileNameExtensionFilter File_ext_txt =
new FileNameExtensionFilter("Text Documents(*.txt)", "txt");
FileNameExtensionFilter File_ext_pdf =
new FileNameExtensionFilter("PDF", "pdf");
FileNameExtensionFilter File_ext_doc =
new FileNameExtensionFilter("Word 97-2003 Document", "doc");
saveFile.addChoosableFileFilter(File_ext_pdf);
saveFile.addChoosableFileFilter(File_ext_doc);
saveFile.addChoosableFileFilter(File_ext_txt);
FileFilter extension = saveFile.getFileFilter();
int userSelection = saveFile.showSaveDialog(null);
File File_Path = saveFile.getSelectedFile();
String fullPath = File_Path.getAbsolutePath();
String Ext = null;
if (userSelection == JFileChooser.APPROVE_OPTION){
if(extension == File_ext_txt){
Ext = "txt";
}
File save = new File(fullPath+"."+Ext);
System.out.println(extension);
flag = save.createNewFile();
}
}
}