Solved

Deleting a file from disk

Posted on 2004-08-27
10
292 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
Comment Utility
on a File object you can call the delete() function
0
 
LVL 19

Accepted Solution

by:
ramazanyich earned 25 total points
Comment Utility
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 50 total points
Comment Utility
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
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
Comment Utility
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
Comment Utility
OK, the above posted code works.

thanks for all the reponses.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Your welcome.
Thanks for accepting.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Glad we could help :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now