aspose file tools
The moose likes Java in General and the fly likes Delete multiple file and multiple directory Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Delete multiple file and multiple directory" Watch "Delete multiple file and multiple directory" New topic
Author

Delete multiple file and multiple directory

somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
My problem :

I want to delete all file/ derectory in current directory, BUT i don't know how to do ..

How to do that ?


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24040
    
  13

Use java.io.File.listFiles() to get an array of File objects representing every file in a directory. For each one, check isDirectory(). If it returns false, you've got a file, and can just delete it. Otherwise, it's another directory, and you have to recursively delete its contents before deleting it.


[Jess in Action][AskingGoodQuestions]
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Ernest Friedman-Hill:
Use java.io.File.listFiles() to get an array of File objects representing every file in a directory. For each one, check isDirectory(). If it returns false, you've got a file, and can just delete it. Otherwise, it's another directory, and you have to recursively delete its contents before deleting it.


Mr.Ernest,
Recursive deletion like this way is pretty exhaustic, I feel... We have to keep track of all the directories... Even after we delete all the contents inside a directory, we need to get back to its parent directory and delete the original directory. If the directories are located in a very depth way, it's more exhaustic...

Kind of exhaustic depth-first search...

I'm wondering if there is any other way to accomplish somkiat's requirements...

Thanks


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Ian Darwin
author
Ranch Hand

Joined: Aug 03, 2001
Posts: 64
Recursive deletion like this way is pretty exhaustic, I feel... We have to keep track of all the directories... Even after we delete all the contents inside a directory, we need to get back to its parent directory and delete the original directory. If the directories are located in a very depth way, it's more exhaustic...

Kind of exhaustic depth-first search...

I'm wondering if there is any other way to accomplish somkiat's requirements...

Well, recursion isn't really hard, once you understand it. And every Java developer should But meanwhile...

Depending on the operating system, you could also use the system utilities; something like this for UNIX or LInux:

[ October 13, 2004: Message edited by: Ian Darwin ]

Ian Darwin
Many questions are answered in my Java Cookbook, 2nd Edition
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Yes, Mr.Darwin, calling native function using exec() method is a better alternative, since we don't need to do recursion by ourselves and the options of "rm" command will take care of it for us...

Thanks a lot for ur tips as well...
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
What made you arrive at the conclusion that a native call is the better solution? Seldom is the best solution arrived at simply because the alternatives 'too hard'.

Native calls are a last resort; they have been provided for when the JVM can't achieve the intended result (often because the result is platform-dependant). Since when did deleting a directory become platform-dependant?

Sounds like a red herring to me.


Tony Morris
Java Q&A (FAQ, Trivia)
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Delete subir after the files:

- real hard stuff.


http://home.arcor.de/hirnstrom/bewerbung
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Stefan Wagner:
Delete subir after the files:

- real hard stuff.


Imagine if you have several steps deep down into the tree? Depth-first search is exhaustic, as we have learnt in bachelor degree...

It's true that platform-independent issue matters, but we can certainly check the platform before using the exec() in our application... So instead of thining how to dela with multiple steps in directory tree, we can easily pick up a native call and just use it in exec()...

Just my 2 cents...
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

You may convert every recursive algorithm to a non-recursive one.
Which wasn't the question.

I don't know the system-call to delete a subdirectory on a Mac, on Windows, on future-versions of windows, of mobile-phones ...., washing machines, and other java-devices...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24040
    
  13

Originally posted by Ko Ko Naing:

So instead of thining how to dela with multiple steps in directory tree, we can easily pick up a native call and just use it in exec()...


I'm not aware of an OS that has "delete a file tree" as a kernel primitive. It's generally a user-space program (rm on unix, deltree on windows). That external program has no choice but to recurse (or iterate, as Stefan points out) though all the files, exactly as you would in Java. The price you pay for that "convenience" is extra Java code to choose a command based on the OS, plus the overhead of executing a separate process, to do something which will be no faster in native code than in Java (since the runtime is going to be dominated by disk I/O performance.)

This is the weirdest argument I've seen in a long time.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Delete multiple file and multiple directory
 
Similar Threads
files in java
How to pass more than 1 parameters into the sevlet???
Deleting multiple files
Multiple excludes for ant copy task
How to delete all existing files in a dierectry?