vendredi 20 février 2015

Saving already existing file with added file extension not prompting overwrite message



For my GUI project that I am doing, I am trying to get the Save mechanisms down nicely. I am currently working on Overwriting files, and I've run into a bit of a problem.


Here's a test case that I've created:


Save current file using Save As. When prompted for a file name, give an existing file name with an added file extension.


Let's assume that an already existing file is called: test.config


If I type in: test and press save, an overwrite prompt pops up, which is what I want.


If I type in: test.config and press save, NO overwrite prompt pops up, and the program continues. This is not what I want. I want an overwrite prompt to pop up and warn the user.


I've been searching around various websites and Javadocs for Files and I can't seem to figure out how to check if the file exists if the user types in an extra extension.


Here is some code that might help you guys understand:



...

int returnVal = fc.showSaveDialog(Dialogc.this);
if(returnVal != JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null,
"Error saving file. \nFile not saved.");
return 1;
}

// Get file name
file = fc.getSelectedFile();

/* Check if file exists */
if(file.exists()) {
JOptionPane.showConfirmDialog(null,
"File already exists. Overwrite?", "Overwrite Prompt",
JOptionPane.YES_NO_OPTION);
}
else {
// If it doesn't exist, create new file
}
...


NOTE: I do have methods that add or remove the extension. Should I use these to maybe help with checking if the given file already exists?




Aucun commentaire:

Enregistrer un commentaire