Solved

Deleting a file from disk

Posted on 2004-08-27
10
308 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 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 65
eclipse argument 14 60
ForLoop Example 3 47
java jdbc batch example 8 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

816 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

10 Experts available now in Live!

Get 1:1 Help Now