?
Solved

Deleting a file from disk

Posted on 2004-08-27
10
Medium Priority
?
329 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

771 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