?
Solved

Preventing Duplicate File names to disk.

Posted on 2005-03-23
7
Medium Priority
?
253 Views
Last Modified: 2011-09-20
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
Comment
Question by:hugoitt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 13612871
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
 

Author Comment

by:hugoitt
ID: 13613697
thanks zzynx, that worked. Now I need help with the first part.
0
 
LVL 6

Expert Comment

by:guitaristx
ID: 13613807
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:hugoitt
ID: 13613982
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
 
LVL 37

Accepted Solution

by:
zzynx earned 2000 total points
ID: 13619464
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
 

Author Comment

by:hugoitt
ID: 13623858
Thanks zzynx, that worked.

thanks for all the responses.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13628303
Thank you
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question