• Post Reply Bookmark Topic Watch Topic
  • New Topic

searching a file in hierarchy and delete  RSS feed

 
karan sharma
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
i have to delete a file from a directory structure .i donot know where is file stored in my folder hierarchy .i have to search that file and delete that file .

for example ;
i have a folder name A , which have folder B ,C ,D ,E . B folder have other 2 folder or 3 file (anything) , same C and D And E can have file or folder . means we donot know that A folder have how many files or folder in it . in that we have to search a specific file and delete it .


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All that is necessary to do this (listing files and subdirectories in a directory and deleting a file) is in the API of the class java.io.File. So lookup the documentation for that class.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karan replied to me in a private message, which I'm inluding below:

Note: Karan, please use the forums instead of private messages. If you use the forums, everybody can read your message and help you.

Karan wrote:

hi ,

thanks for your suggestion . i read all API .

but i was expecting more then that .

i made one code , this is working for one level only(1 depth).can you please make it recursive or make it for search for more level .



hope you are sending solutions .

What did you try yourself to make it recursive? Do you understand how to write a recursive method? If not, search around on Internet or in books about how to write a recursive method.

The basic idea what a recursive method should do is this:

- List all the files and directories in the given directory (the directory should be supplied as a method argument).
- Loop through all the files and directories in the list. For each file or directory:
- Is it a directory? If yes, then call the method itself (recursion) with that directory as the argument.
- Is it the file we are looking for to delete? If yes, then delete the file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!