• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 2653
18
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
umasankar puranam
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you want to use recursion? For any tree-like structure, including files and folders, recursion is the obvious and easiest mechanism.
 
author
Posts: 23836
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Jan Cumps
Bartender
Posts: 2653
18
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 23836
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!