Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Java - Deleting File

                         

Introduction

In this section, you will learn how a specified file or directory is deleted after checking the existence of the. 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 takes an input for the file name to be deleted and 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

                         

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

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

i have a file with 3 fields and 10 records. if i want to update 5th record what should i do?
do i required to delete the whole file? otherwise how?

Posted by soujanya on Tuesday, 07.29.08 @ 15:29pm | #69771

fantastic tutorials im from finland and i study java. Great examples than my country programming books content

Posted by somebody on Thursday, 05.15.08 @ 00:31am | #59964

Latest Searches:
creating eclipse insta
b tree implementatio
get record using chara
ColdFusion Development
Combattons la programm
DFD
validating form using
Linux Caixa MаÐ??аÐ?
JSTL tutorials
Create JTree using XM
AND Operator
jdbc connections
CSS CSS Customize the
ะ ย ะà¸???ะÂ
net use
how to include jsf tag
IEEE Projects
mysql query
dwr reverse ajax
server dojo
Java Development Hiber
ะà???ะ ยà¸?ะà
PHP Randomizing Random
Hibernate Min() Functi
Photoshop Drawing 3D m
jdbc html jsp
login authentication
annotation in jee
How to read XML
excel style sheet
array to string
Photoshop Cut out imag
Facelet tutorial
Exercise - Pad Left
JSP:File Input Paramet
tables pdf itext
how will we set enviro
emailing file using js
MVC Architecture
form bean
howtoretrievedatabasei
weblogic 10.3
download tomcate 5.0
project for mobile pla
<html:options>
Ktoolbar TUTORIAL
drop box
rules
StringTokenizer specif
Date Examples java
basic about struts
Jtable Jbutton
sequence number genera
IBM MQ series
java is useful in netb
strating a thread in j
axis2 and ms exchange
Java example program t
fjhjf
median
Jtable
JSF datatree
Dependency list box in
jQuery To Slide Effect
java string
org.apache.poi.hssf.us
program for display th
passing values from js
PL/]SQL
selectonelistbox examp
Wireless
adding calculation
struts DAO connection
save query string
pseudocode
Tomcat deploy JSP
Load
sort object compareTo
source code of puzzle
sum of n integers
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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 | 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.