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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java - Deleting the file or Directory 
 

This example illustrates how to delete the specified file or directory after checking weather the file exists or not.

 

Java - Deleting the file or Directory

                         

This example illustrates how to delete the specified file or directory after checking weather the file exists or not. This topic is related to the I/O (input/output) of java.io package.

In this example we are using File class of java.io package. The File class is an abstract representation of file and directory pathnames. 

Explanation

This program deletes the specified file if that exists. We will be declaring a function called deletefile which deletes the specified directory or file.

deletefile(String file)

The function deletefile(String file) takes file name as parameter. The function creates a new File instance for the file name passed as parameter

File f1 = new File(file);

and delete the file using delete function f1.delete(); which return the Boolean value (true/false). It returns true if and only if the file or directory is successfully deleted; false otherwise.

delete()
Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.
Returns:
true if and only if the file or directory is successfully deleted; false otherwise

Code of the Program :

import java.io.*;

public class DeleteFile{
  private static void deletefile(String file){
    File f1 = new File(file);
    boolean success = f1.delete();
    if (!success){
      System.out.println("Deletion failed.");
      System.exit(
0);
    }
else{
      System.out.println(
"File deleted.");
 
   }
  }
 
 public static void main(String[] args){
    switch(args.length){
   
   case 0: System.out.println("File has not mentioned.");
          System.exit(0);
     
 case 1: deletefile(args[0]);
 
         System.exit(0);
      default : System.out.println("Multiple files are not allow.");
            System.exit(0);
    }
  }
}

Download File Deletion Example

                         

» View all related tutorials
Related Tags: c file class orm constructor object path struct const value name return instance paths create if send match to bot

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 

Current Comments

9 comments so far (
post your own) View All Comments Latest 10 Comments:

can u help me to know can we delete the directory on machine using java

Posted by mayur on Wednesday, 12.17.08 @ 04:59am | #82863

public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}

// The directory is now empty so delete it
return dir.delete();
}

Posted by Farhan on Wednesday, 10.29.08 @ 17:36pm | #81393

Hai!
This is Balaji, i am pursuing my MCA and i am presently doing project on java, so i am stuck in a problem, when i select some components like radiobutton or type some text in textfield when i click search button it has to search those selected components in the harddisk based on the partitions in windows that has been previously present as a folder, can u help me in solving this problem

Posted by balaji on Wednesday, 04.2.08 @ 10:04am | #55097

import java.io.*;
import java.util.*;

public class DeleteAny
{
public static void main(String args[])throws IOException
{
File f[]=new File[args.length];

for(int i=0; i<f.length; i++)
{
f[i]=new File(args[i]);
delete(f[i]);
}

}

static void delete(File f)
{try{System.out.println(";;"+f.getName()+" "+f.listFiles().length);

if(f.listFiles().length>1)
{
File fd[]=f.listFiles();

for(int i=0; i<fd.length; i++)
delete(fd[i]);
}
if(f.listFiles().length<1)
{ if(!f.canWrite())
{
if(f.isDirectory()|f.isFile())
f.delete();
}
}
}
catch(Exception e)
{
System.out.println("FILE NOT FOUND");
e.printStackTrace();
}

}
}

Posted by ankur on Sunday, 12.9.07 @ 20:18pm | #41666

you have done many erros in it..
1..>if the file will writeprotected then it will not delete by this programme..
2..>and ofcurese it will delete only single directory or file if directory content subdirectoris then it will not delete it ..as u knw
3>>>look at my code it will all subdirectes n files
but yaa there is an error in code find it actually it is not deleting files find n tell me why..
..................................
import java.io.*;
import java.util.*;

public class DeleteAny
{
public static void main(String args[])throws IOException
{
File f[]=new File[args.length];

for(int i=0; i<f.length; i++)
{
f[i]=new File(args[i]);
delete(f[i]);
}

}

static void delete(File f)
{try{System.out.println(";;"+f.getName()+" "+f.listFiles().length);

if(f.listFiles().length>1)
{
File fd[]=f.listFiles();

for(int i=0; i<fd.length; i++)
delete(fd[i]);
}
if(f.listFiles().length<1)
{ if(!f.canWrite())
{
if(f.isDirectory()|f.isFile())
f.delete();
}
}
}
catch(Exception e)
{
System.out.println("FILE NOT FOUND");
e.printStackTrace();
}

}
}

Posted by ankur on Sunday, 12.9.07 @ 20:16pm | #41665

please any body reply me how to delete a folder using java

Posted by rajathi on Monday, 11.12.07 @ 18:10pm | #36974

how to delete a file with given prefix

Posted by tarun on Monday, 08.6.07 @ 18:59pm | #22776

This program can also delete a file. Very helpful. Many thanks

Posted by Kimtran on Wednesday, 06.13.07 @ 04:20am | #19055

i love this

Posted by benjjamin on Thursday, 12.7.06 @ 23:25pm | #603

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.