Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

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());
        }
   }
0
hugoitt
Asked:
hugoitt
  • 3
  • 3
1 Solution
 
zzynxSoftware engineerCommented:
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());
}
0
 
hugoittAuthor Commented:
thanks zzynx, that worked. Now I need help with the first part.
0
 
guitaristxCommented:
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
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hugoittAuthor Commented:
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
0
 
zzynxSoftware engineerCommented:
Have a look at
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html and
http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html

I think you need something like:
int choice = OptionPane.showConfirmDialog(yourFrame, "File already exists, would you like to replace the existing File?",
    "the Title of the window", JOptionPane.YES_NO_OPTION);

if (choice == JOptionPane.YES_OPTION) {
}
0
 
hugoittAuthor Commented:
Thanks zzynx, that worked.

thanks for all the responses.
0
 
zzynxSoftware engineerCommented:
Thank you
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now