It worked best for me
FileChooser choose = new FileChooser();
choose.getExtensionFilters().add(new FileChooser.ExtensionFilter("Text doc(*.txt)", "*.txt"));
choose.setInitialFileName("*.txt");
File file = choose.showSaveDialog(stage);
if (file != null) {
if (file.getName().endsWith(".txt")) {
} else {
throw new Exception(file.getName() + " has no valid file-extension.");
}
}
Manual replacement problem manually:
if(!f.getName().contains(".")) {
f = new File(f.getAbsolutePath() + ".txt");
}
It means that the file without the extension may not exist, but if the file exists with the extension, it was overwritten without warning. not expected behavior.
source
share