Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

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


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Beginners DeleteFile

User Comments
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl
  Blink   Furl   Simpy   Y! MyWeb 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

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

the method delete() will delete only a directory but not a file for instanse

package ioDemo;

import java.io.File;

public class FileDemo
{

public static void main(String[] arg)
{

File f1 = new File("C:/Documents and Settings/home/Desktop/javaProgs/Test");

File f2 = new File("C:/Documents and Settings/home/Desktop/javaProgs","Test1");

System.out.println(f2.renameTo(f1)+" "+ f1.getName()+"\n"+f2.isDirectory()+" "+f2.getName() );

System.out.println("Items Deleted\n "+f1.getName()+" "+f1.delete()+"\n"+f2.getName()+" "+f2.delete());
}
}
this programe can not delete f2 which is a txt file, it will return false.
Please expain me how could I delte a txt file using FILE class

Posted by rasool on Saturday, 02.6.10 @ 15:06pm | #94624

recursive delte:
in java,only a empty folder canbe deleted,so you must first delte files in folder,then delete the empty folder.

Posted by bruce on Monday, 11.16.09 @ 12:19pm | #92609

Hello,
I am not able to use ResultSet(ref. rs) two times within the same function. After using while(rs.next()), shows resultset is closed. I am not able to use that rs ref. again and again in the same func. please help me out....

Posted by Parvesh on Tuesday, 09.15.09 @ 13:26pm | #90931

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

more example of deleting files code,,

Posted by aaron on Friday, 11.21.08 @ 19:18pm | #81904

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

I want to delete a folder which contain PDF, pS and like files also at a same time i will have to run a procedure also

Posted by momin hasanabbas on Saturday, 07.5.08 @ 15:32pm | #65815

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

its goods to learn for new people.

Posted by Jay Mishra on Tuesday, 12.11.07 @ 18:30pm | #41844

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

Ask programming questions?

 

 

Back to Tutorial

 

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
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

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

Copyright 2007. All rights reserved.