JavaFX 2.2 Get selected file extension


I don't know how to get selected file exstesion. I have seen that developers ask this question frequently, but I could find and proper answer. I know it is a bug (f, but anyway is there any solutions. So I wanna save my linechart as pdf or png. My code for both files works fine. But I just can't stand not to get selected file. Is there any sollution possible. I see that Java has built in function for this issue.

This solution is not relevant for me: JavaFX FileChooser not returning file extension for Windows

My code is:

 FileChooser fileChooser = new FileChooser();
 FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PDF file(*.pdf)" ,"      

 extFilter = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.png");
 fileChooser.setTitle("Save as");
 File file = fileChooser.showSaveDialog(primaryStage);

Then I get file path without extensions:

 String filePath = file.getAbsolutePath();
5/23/2017 12:23:53 PM

Accepted Answer

If you want to know the extension of selected file from file chooser here is a code..

String fileName = file1.getName();          
String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1, file1.getName().length());
System.out.println(">> fileExtension" + fileExtension);

And here is a brief of what you need to do with file chooser,

FileChooser fileChooser = new FileChooser();
// Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Image Files", "*.jpg", "*.jpeg");

File file = fileChooser.showOpenDialog(root.getScene().getWindow());

if (file != null) {

String fileName = file.getName();           
String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1, file.getName().length());
System.out.println(">> fileExtension" + fileExtension);

6/25/2018 3:35:56 PM

This is a pretty annoying thing in JavaFX if you ask me - because they will automatically append the extension on Windows, but not on Linux or Mac.

So, if you want to be sure that the file you will create will have an extension, you need to do something like this:

FileChooser fc = new FileChooser();
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("Text Files (*.txt)", "*.txt"));
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("XML Files (*.xml)", "*.xml"));
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("All Files (*.*)", "*"));
File file = fc.showSaveDialog(rootPane.getScene().getWindow());
if (file != null)
    File f;
    String tempPath = file.getCanonicalPath().toLowerCase();
    if (!(tempPath.endsWith(".txt") || tempPath.endsWith(".xml")))
        String extension = fc.selectedExtensionFilterProperty().get().getExtensions().get(0).substring(1);
        // default to .txt, if the user had *.* selected
        if (extension.length() == 0)
            extension = ".txt";
        f = new File(file.getCanonicalPath() + extension);
        f = file.getCanonicalFile();

    if (f.exists())
        System.err.println("You will overwrite!");

Note, since we are potentially using a different file name than what came out of the file chooser, the user may not have been prompted about overwriting an existing file - so you will have to handle that check manually as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow