Link to home
Start Free TrialLog in
Avatar of SonicYouth
SonicYouth

asked on

how to recursive del a dir

 i want to del a dir whether it contains or not contains
files or sub dirs!!
  please give me the source code
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Basically the same as objects comment -- I'm a little slower because I _did_ test it.

import java.io.*;

public class Delete {
    private static final boolean verbose = true;

    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java Delete <file or directory>");
            System.exit(0);
        }

        try {
            // delete and display errors
            delete(new File(args[0]));
        } catch (IllegalArgumentException e) {
            System.err.println(e.getMessage());
        }
    }

    public static void delete(File file) {
        // check existence and access mode
        if (file.exists() == false) {
            abort("File not found: " + file.getAbsolutePath());
        }
        if (file.canWrite() == false) {
            abort("File write protected: " + file.getAbsolutePath());
        }

        // recursively descend and delete tree
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            int num = files.length;
            for (int i = 0; i < num; ++i) {
                try {
                    delete(files[i]);
                } catch (IllegalArgumentException e) {
                    System.err.println(e.getMessage());
                }
            }
            if (file.list().length > 0) {
                abort("Directory not empty: " + file.getAbsolutePath());
            }
        }

        // attempt to delete this target
        debug("Attempting to delete: " + file.getAbsolutePath());
        if (file.delete() == false) {
            abort("Delete failed: " + file.getAbsolutePath());
        }
    }

    static void abort(String msg) throws IllegalArgumentException {
        throw new IllegalArgumentException(msg);
    }

    static void debug(String msg) {
        if (verbose) {
            System.out.println(msg);
        }
    }
}

Best regards,
Jim Cakalic
Jim,

You must have too much spare time :-)
Avatar of yongsing
yongsing

SonicYouth has previously asked this question and it has already been answered. However, he/she refused to grade it and now is asking the same question again.

https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20106480

He/She has only graded 23 out of 55 questions asked. I have reminded him/her regarding his/her grading record but he/she chooses to ignore it.
>> You must have too much spare time :-)

Indeed. You are not going to get anything out of this, judging by SonicYouth's record.
Look this is getting pretty tedious now!

Grade your answered questions, or reject the answers.  Don't ask the same question again.  I have already
answered this question, in full, and I am still waiting for the points.

SonicYouth you have been added to my s**tlist: https://www.experts-exchange.com/jsp/memberProfile.jsp?mbr=chrisos

Chrisos
Look this is getting pretty tedious now!

Grade your answered questions, or reject the answers.  Don't ask the same question again.  I have already
answered this question, in full, and I am still waiting for the points.

SonicYouth you have been added to my s**tlist: https://www.experts-exchange.com/jsp/memberProfile.jsp?mbr=chrisos

Chrisos
Hmm. I rarely look at grading record when I answer a question. I usually consider how interesting is the question? Do I know the answer? If not, is it something I am interested in researching? How many points are being offered (i.e., how important is the answer to the person asking)?

In SonicYouth's defense, many of the ungraded questions in the Java forum have been asked during the exchange's dark days. Perhaps they were simply forgotten. Many, in the Java forum and the dozen others in which s/he has ungraded questions, on review don't have very good responses. I'm not sure I would have graded them myself -- not that I ask a lot of questions.

Anyway, "flipping the bozo bit" isn't gonna do anybody much good. SonicYouth, perhaps when you have time you should consider reviewing your question history, accessible from your member profile page. If there are questions that deserve to be graded, well and good. But if old questions have grown stale without acceptable answers, you could ask community support to delete them and restore your points. In the meantime, I hope you find some of the comments here useful.

Best regards -- to all,
Jim Cakalic
Force/accepted in preparation for suspending SonicYouth's account by
Netminder
Community Support Moderator
Experts Exchange