Im making a desktop application and it has a JFileChooser(ShowSaveDialog) function..
When I tried to save a sample text file the program didnt get the extension file that I chose.. I'm trying to use the if else or switch statement and I cant figure it out what command will I use to get the string/Int value for the condition if pdf,word or txt extension is chosen as file extension...
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();
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…