• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to delete all existing files in a directory?

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody!

I want to write a file to a directory / folder.
Before doing it,
I want to delete all existing files in that particular folder.
Can anyone please let me know how to do it?

Regards,
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U can use File.list() method to list all the filenames in a dir and then file.delete to delete the same.
Sample code is given below.



regards
Sri
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks SriRam,
That was exactly what i wanted. My problem is solved.

Thanks
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ashvini Devi:
Thanks SriRam,
That was exactly what i wanted. My problem is solved.

Thanks



Note that File.delete returns a boolean indicating success, which you may want to attend to. Also, can the directory contain other directories? In that case, recursion will be required.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!