Solved

Deleting a file from disk

Posted on 2004-08-27
10
327 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 25 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 50 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 92

Assisted Solution

by:objects
objects earned 50 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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

724 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