aspose file tools
The moose likes Java in General and the fly likes Is it possible to delete all files and folders in a drive non-recursively? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Is it possible to delete all files and folders in a drive non-recursively?" Watch "Is it possible to delete all files and folders in a drive non-recursively?" New topic
Author

Is it possible to delete all files and folders in a drive non-recursively?

umasankar puranam
Ranch Hand

Joined: Sep 24, 2004
Posts: 31
I know that all files and directories and all subdirectories under a drive can be deleted programatically using recurssion. Is it possible to do the same non-recursively? If yes, please give the sample code or if not possible state the reason?

I faced this question in an interview and I said its not possible to delete non-recursively? Am I currect?


umasankar<br />Software Engineer
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2343

The File class only allows you to delete empty dirs, so yes: this is typically done by recursively visiting lower directories and emptying them.
But you could also start a native operation system command from java that removes a dir that is not empty and its subdirs (e.g. rm -fr in unix). That would be a non-recursive solution.


OCUP UML fundamental
ITIL foundation
Dave Lorde
Greenhorn

Joined: Apr 02, 2007
Posts: 20
In principle, any recursive procedure can be achieved non-recursively, by using an explicit stack instead of the implicit stack of recursion.

See Convert Recursive to Non-recursive.
umasankar puranam
Ranch Hand

Joined: Sep 24, 2004
Posts: 31
Hi Dave Lorde,

Could you please give me sample non-recursive code in Java to delete all files, directories, and sub-directories in a drive?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Jan Cumps wrote:(e.g. rm -fr in unix). That would be a non-recursive solution.

I wouldn't be too sure. The recursion may not be in your Java program, but it will probably be moved to the native program.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
umasankar puranam
Ranch Hand

Joined: Sep 24, 2004
Posts: 31
Hi Rob Prime,

I need a solution not by running an unix command, I need it using java code? Could you, or anybody in the forum please provide sample java code for doing this?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Why don't you want to use recursion? For any tree-like structure, including files and folders, recursion is the obvious and easiest mechanism.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

Rob Prime wrote:Why don't you want to use recursion? For any tree-like structure, including files and folders, recursion is the obvious and easiest mechanism.


Agreed. Most of the time recursion is avoided (assuming that its the best option) because the algorithm doesn't have an upper capacity in memory usage. In this case, I highly doubt you will encounter a case where the directory structure is so deep that you'll run out of heap.... And it's also the best option too.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2343

Rob Prime wrote:I wouldn't be too sure. The recursion may not be in your Java program, but it will probably be moved to the native program.
True. I should have read the man page for the -r option:
-r, -R, --recursive remove the contents of directories recursively

It's a giveaway, isn't it?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19


Sorry... didn't read the question correctly. It's an interview question. Anyway....


Having an example on how to delete a directory, in a non-recursive manner, misses the point. Remember back to your old algorithms class? when you first learned about recursion? Remember all that work that you did prior to using recursion, with stack based data structures, to do stuff, that became really easy once you added recursion to your arsenal?

That's the point of the interview question. The interview was likely testing you to see if you understood the basics of recursion, which includes an understanding on how to do recursion, without the language making it so easy for you to do it -- to simulate it using data structures.

Henry
Dave Lorde
Greenhorn

Joined: Apr 02, 2007
Posts: 20
umasankar puranam wrote:Hi Dave Lorde,

Could you please give me sample non-recursive code in Java to delete all files, directories, and sub-directories in a drive?


Hi Umasankar Puranam,

No, I couldn't. It is possible but pointless. If you would like to do it for yourself, by all means go for it, I'm sure you'd learn a lot. Try it with a very simple example to start.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to delete all files and folders in a drive non-recursively?
 
Similar Threads
Directory deleting
Dynamic JTree with checkbox for all system files
creation and deletion of files and folders.
Delete Directory
how to read non-text files