Link to home
Start Free TrialLog in
Avatar of hugoitt
hugoitt

asked on

Preventing Duplicate File names to disk.

I would like to add a test that checks for duplicate file names when i write to disk, I would like a dialog box that pops
up and says "File already exists, would you like to replace the existing File?". Also, I need a test that checks the combo box
to see if the file name already exists, i do not want the same name in the combo box twice.


OICSDisplay: Save(File file) A test must be included to prevent duplicate file names to be written to the script select combo box



    /**
     *Shows the save dialog window.
   */  
    public void scriptFileSave()
    {
        saveDialog.setVisible(true);
        if (saveDialog.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
        {
            //Make sure the extension of the file is .script
            File tempFile = saveDialog.getSelectedFile();
            File finalFile;
            String tempFileName = tempFile.getPath();
            if(tempFileName.endsWith(".script"))
            {
                //File already ends with .script
                finalFile = tempFile;
            }
            else
            {
                //File does not end with .script, change it
                finalFile = new File(tempFileName + ".script");
            }
            scriptFileSave(finalFile);
       }        
    }
    /**
     *When the save button is pushed in the Script Panel, this routine will save
     *the contents of the list box which is contained in a vector to a file on the disk drive.
  */  
    private void scriptFileSave(File file)
    {
        try
        {
            // Write the objects stored in script vector to the specified file
            ObjectOutputStream Script = new ObjectOutputStream(new FileOutputStream(file));
            Script.writeObject(scriptVector);
            Script.flush();
            Script.close();
            scriptSelectComboBox.addItem(file.getName());
            scriptSelectComboBox.setSelectedItem(file.getName());
            // Display the status of the save file operation in jlblStatus
            jlblStatus.setForeground(blackForegroundText);
            jlblStatus.setText(file.getName()  + " SAVED ");
            systemLogging.systemMessage("SCRIPT SAVED -  " + file.getName());
        }
        catch (IOException ex)
        {
            jlblStatus.setForeground(redForegroundText);
            jlblStatus.setText("ERROR SAVING " + file.getName());
            systemLogging.systemMessage("SCRIPT SAVED ERROR -  " + file.getName());
        }
   }
Avatar of zzynx
zzynx
Flag of Belgium image

The easiest first ;°)
>> i do not want the same name in the combo box twice.
Use a Set to avoid duplicates.

Set theSet = new HashSet();

if ( !theSet.contains(file.getName() ) {
    scriptSelectComboBox.addItem(file.getName());
    theSet.add(file.getName());
}
Avatar of hugoitt
hugoitt

ASKER

thanks zzynx, that worked. Now I need help with the first part.
Are you talking about java.io.File.exists():
http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#exists()
?

You're using java.io.File objects, right?

if(file.exists())
{
     //file exists!! do something
}
else
{
     //save the file, or whatever else needs done
}
Avatar of hugoitt

ASKER

Actually guitaristx, I already know how to check if a file exists on the disk, the problem I'm having is how to create
a custom dialog that will alert the operator of the decision he is about to make. I'm using JFilechooser for my open and
save dialogs, don't quite know how to create a custom dialog.

The dialog would have a yes and no button and I would copy over the existing file on disk depending on the response.

thanks
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hugoitt

ASKER

Thanks zzynx, that worked.

thanks for all the responses.
Thank you