[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Deleting a file from disk

Posted on 2004-08-27
10
Medium Priority
?
331 Views
Last Modified: 2010-03-31
Hello,

I'm saving a file to disk using the following code. My question is how do I delete the file? I would have a delete
button, which when pressed would read the currently selected file from a combo box and delete the file from disk.



    // Open File
    public void save()
    {
        jFileChooser1.setVisible(true);
        if (jFileChooser1.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
        {
            save (jFileChooser1.getSelectedFile());
        }
    }
   
    private void save (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());
         // Display the status of the save file operation in jlblStatus
         jlblStatus.setText(file.getName()  + " Saved ");
      }
      catch (IOException ex)
      {
        jlblStatus.setText("Error saving " + file.getName());
      }
   }


Here is my delete button code:
   private void scriptDelScriptButtonActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (scriptSelectComboBox.getSelectedIndex() != 0)
        {
           Object newItem = scriptSelectComboBox.getSelectedItem();
           scriptSelectComboBox.removeItem(newItem);
           jlblStatus.setText(newItem.toString()  + " Deleted ");
     // Need to add code here that will delete the file from disk

        }
    }
0
Comment
Question by:GoldStrike
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 11918399
on a File object you can call the delete() function
0
 
LVL 19

Accepted Solution

by:
ramazanyich earned 100 total points
ID: 11918403
You should use File.delete() API call.
In you code first you should remember user selection as class member and then use it  then user clicks on delete button.

For example:
//define member
File curfile = null;

private void save (File file){
curfile = file;
....

        jlblStatus.setText(newItem.toString()  + " Deleted ");
     // Need to add code here that will delete the file from disk
       curfile.delete();
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 200 total points
ID: 11918411
So I guess somehting like this should work:

// code that will delete the file from disk:
File f = new File((String)newItem);
f.delete();
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 11918449
you need to use delete() and check the return value to see if the file was actually deleted.

public void delete(File f)
{
   if (!f.delete())
   {
      jlblStatus.setText("failed to delete file " + f.getName());
   }
}

0
 

Author Comment

by:GoldStrike
ID: 11932077
the delete is failing, when I look in the debugger, the value for f is
correct "d:\\optics\\system\\titan.script".

I'm thinking that it's trying to delete a directory, but since the directory is not empty it is failing.
Do I need to traverse through the files in the directory first until I find the file I need to delete?

thanks
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11932225
>> thinking that it's trying to delete a directory
Is "d:\\optics\\system\\titan.script" a directory or is it a file?
If it's a file, it is trying to delete that file. But maybe there are OS reasons why it can't delete it.

>> Do I need to traverse through the files in the directory first until I find the file I need to delete?
Why should you if you have the file name (got from the file chooser)?
0
 

Author Comment

by:GoldStrike
ID: 11933062
I;m deleting a file. I'm actually getting the file name from the combo box as follows.

    private void scriptDelScriptButtonActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (scriptSelectComboBox.getSelectedIndex() != 0)
        {
           Object newItem = scriptSelectComboBox.getSelectedItem();
           File f = new File(fFileRef.getPath() + "//scripts//" + newItem.toString());
           delete(f, newItem);
        }
    }

    public void delete(File f, Object newItem)
   {
      if (!f.delete())
      {
         jlblStatus.setText("failed to delete file " + f.getName());
      }
      else
      {
         jlblStatus.setText(f.getName() + " Deleted ");
         scriptSelectComboBox.removeItem(newItem);
      }
   }
0
 

Author Comment

by:GoldStrike
ID: 11934160
OK, the above posted code works.

thanks for all the reponses.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11936191
Your welcome.
Thanks for accepting.
0
 
LVL 92

Expert Comment

by:objects
ID: 11936549
Glad we could help :)
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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month17 days, 21 hours left to enroll

829 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