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_OPTIO N)
{
//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(scriptV ector);
Script.flush();
Script.close();
scriptSelectComboBox.addIt em(file.ge tName());
scriptSelectComboBox.setSe lectedItem (file.getN ame());
// Display the status of the save file operation in jlblStatus
jlblStatus.setForeground(b lackForegr oundText);
jlblStatus.setText(file.ge tName() + " SAVED ");
systemLogging.systemMessag e("SCRIPT SAVED - " + file.getName());
}
catch (IOException ex)
{
jlblStatus.setForeground(r edForegrou ndText);
jlblStatus.setText("ERROR SAVING " + file.getName());
systemLogging.systemMessag e("SCRIPT SAVED ERROR - " + file.getName());
}
}
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
{
//Make sure the extension of the file is .script
File tempFile = saveDialog.getSelectedFile
File finalFile;
String tempFileName = tempFile.getPath();
if(tempFileName.endsWith("
{
//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(scriptV
Script.flush();
Script.close();
scriptSelectComboBox.addIt
scriptSelectComboBox.setSe
// Display the status of the save file operation in jlblStatus
jlblStatus.setForeground(b
jlblStatus.setText(file.ge
systemLogging.systemMessag
}
catch (IOException ex)
{
jlblStatus.setForeground(r
jlblStatus.setText("ERROR SAVING " + file.getName());
systemLogging.systemMessag
}
}
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
}
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
}
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks zzynx, that worked.
thanks for all the responses.
thanks for all the responses.
Thank you
>> 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.getN
scriptSelectComboBox.addIt
theSet.add(file.getName())
}