Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Delete file or Directory 
 

In this example we are discussing the deletion of a file or a directory by using a java program.

 

Delete file or Directory

                         

In this example we are discussing the deletion of a file or a directory by using a java program. We pass the file name or the directory name to which we want to delete. The given program deletes an existing file if the file is not write protected. this example also deletes an existing directory if the directory is empty otherwise delete operation fails.

Description of program:  

In the given example first of all we check the string array variable str for its length, if we pass a command line argument then its value will not be null and rest of the operation is performed. After that we are creating a delete() method. In this method first we are creating a file object that contains the file or directory name to be deleted. Then we check for file whether it exists or not and if it exists checks whether it is write protected or not and calls the delete() on this file, If it is not a file then we check for the directory and also checks whether it is empty or not and after that we are calling the delete() method on this object and takes the result in a Boolean type variable success. If this variable has the value true then the given directory is deleted successfully otherwise If condition gets executed and the message Deletion: deletion failed is displayed. In our case we are creating a directory named delte that is not empty and then we are trying to delete it, thats why we got the message Can not delete as directory is not empty: delte.

Here  is the code of this program:

import java.io.*;

public class DeleteOperation {

  public static void main(String[] str) {

    if (str.length != 1) {    
      System.err.println("Deleting file or directory");
      System.exit(0);
    }    
    try 
    delete(str[0]); 
    }
    catch (IllegalArgumentException e) {
      System.err.println(e.getMessage()); 
      }
  }
  public static void delete(String filename) {

    File file = new File(filename);

    if (!file.exists()) fail("No such file or directory 
                              exist: " + filename);

    if (!file.canWrite()) fail("Can not delete as it is write 
                                protected: " + filename);

    if (file.isDirectory()) {
      String[] files = file.list();
      if (files.length > 0) fail("Can not delete as directory 
          is not empty: " + filename);
    }
    boolean success = file.delete();    
    if (!success) fail("Delete: deletion failed");
  }
  protected static void fail(String msg) throws IllegalArgumentException {
    throw new IllegalArgumentException(msg);
  }
}

Here is the output:

C:\Examples>java DeleteOperation delte
Can not delete as directory is not empty: delte

Download of this program.

 

                         

» View all related tutorials
Related Tags: c orm form time script object io objects help method sed system ip collection this opera create show for work

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.