Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Java - Deleting the file or Directory

 
 

Share on Google+Share on Google+

Java - Deleting the file or Directory

Advertisement
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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2007

Related Tutorials

Discuss: Java - Deleting the file or Directory   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
usha
March 28, 2011
total loss data

please help me, i need java source code about total lost data in hard drive. how to find the total deleted data ih hard drive using java programming... this my project...pls help me..
usha
March 28, 2011
total loss data

please help me, i need java source code about total lost data in hard drive. how to find the total deleted data ih hard drive using java programming... this my project...pls help me..
Mohd. Aftaab Siddiqui
November 12, 2011
directory delete

i want to delete a directory that is not empty....please show this program.
Amit kumar
January 8, 2012
delete nesting file in java

How to delete nesting file in java
DMCA.com