Delete directory in java


 
Thread Tools Search this Thread
Top Forums Programming Delete directory in java
# 1  
Old 06-09-2012
Delete directory in java

Hello,

Please, how can i delete a directory which contain directories and files (in Java)?

Thank you so much for help
# 2  
Old 06-13-2012
java code:
  1. import java.io.File;
  2.  
  3. public class TestClass {
  4.     public static void main(String[] args) {
  5.         File f = new File("/path/to/dir");
  6.         deleteDir(f);
  7.     }
  8.    
  9.     public static void deleteDir(File x) {
  10.         System.out.println(x);
  11.         if (x.isDirectory() && x.list().length > 0) {
  12.             for (String y: x.list()) {
  13.                 File f = new File (x.getAbsolutePath() + "/" + y);
  14.                 deleteDir(f);
  15.             }
  16.             System.out.println("Deleting " + x);
  17.             x.delete();
  18.         }
  19.         else {
  20.             System.out.println("Deleting " + x);
  21.             x.delete();
  22.         }
  23.     }
  24. }
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 06-15-2012
Use Apache Commons FileUtils

If you don't want to write your own code for common task, Use Apache commons FileUtils class , it has method
deleteDirectory(File directory)
FileUtils (Commons IO 2.3 API)
This User Gave Thanks to javabuddy For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Shell Programming and Scripting

Cannot run this .java file from the home directory??

I wrote a simple test.java program in vi. I know it compiles correctly because I went into the directory where test.java was and compiled it and it created a java.class. I then ran test.java by staying in the same directory where it was and it worked great. However, when i backed out of the... (3 Replies)
Discussion started by: syregnar86
3 Replies

3. Solaris

Sun Java Directory Server icon

This probably is immensely trivial, but as the bank robber in the movie Dirty Harry says, "I gots to know." In Sun Java Directory Server's (v 5.2) list of users and groups, there are several scores of usernames that have an icon that looks like a blue circle. Below that are scores of usernames... (0 Replies)
Discussion started by: chilinski
0 Replies

4. Shell Programming and Scripting

How to run the Shell Script from external directory using java?

Hi, I have created a Shell Script and invoke through java using Process Builder It's working fine, if (Shell script file ) in the same directory as java file. By Problem: How to run the Shell Script file( resides in external directory) using java. What configuration i have... (1 Reply)
Discussion started by: nanthagopal
1 Replies

5. Shell Programming and Scripting

how to delete certain java script from html files using sed

I am cleaning forum posts to convert them in offline reading version with clean html text. All files are with html extension and reside in one folder. There is some java script i would like to remove, which looks like <script LANGUAGE="JavaScript1.1"> <!-- function mMz() { var mPz = "";... (2 Replies)
Discussion started by: georgi58
2 Replies

6. Shell Programming and Scripting

How to delete some of the files in the directory, if the directory size limits the specified size

To find the whole size of a particular directory i use "du -sk /dirname".. but after finding the direcory's size how do i make conditions like if the size of the dir is more than 1 GB i hav to delete some of the files inside the dir (0 Replies)
Discussion started by: shaal89
0 Replies

7. Shell Programming and Scripting

How to see java installed directory ? Please help me

Hi All, I am using SunOS 5.8 and KSH shell. My problem is , I can't find my java installed directory. Java is running from /usr/bin folder but I think there java is the link to actual java directory. Also nothin is set to $JAVA_HOME env variable. How to see the actual program location pointed by... (1 Reply)
Discussion started by: Sooraj_Linux
1 Replies

8. Solaris

Sun Java System Directory Server

Hi, Does anyone know if the Sun Java System Directory Server (now Oracle Directory Server) is freely available for download? Thanks (1 Reply)
Discussion started by: Mack1982
1 Replies
Login or Register to Ask a Question