how to search a file from a hierarchy and delete it

hi ,

i have to delete a file from a directory structure .i dno't 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 dnot know that A folder have how many files or folder in it . in that we have to search a specific file and delete it .

[495 byte] By [kkaran007a] at [2007-11-26 15:47:56]
# 1
Create a recursive method which looks for the file.Kaj
kajbja at 2007-7-8 22:07:23 > top of Java-index,Java Essentials,Java Programming...
# 2
http://en.wikipedia.org/wiki/Recursion#Recursion_in_computer_science http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#listFiles()
quittea at 2007-7-8 22:07:23 > top of Java-index,Java Essentials,Java Programming...
# 3
hi Kajbi,thx for reply .but can you give me full code in detail plz . regards
kkaran007a at 2007-7-8 22:07:23 > top of Java-index,Java Essentials,Java Programming...
# 4

> but can you give me full code in detail plz .

People are unlikely to post full code replies here.

Because a working full code for what you're asking

would probably take 10 to 30 minutes to get right.

No one would do that for you for free.

Instead, you should start trying (by following the advice above),

and when you got stuck, you post your partial code,

then people can point out what's wrong/missing.

Suntra_Vineeta at 2007-7-8 22:07:23 > top of Java-index,Java Essentials,Java Programming...
# 5

hi vineet ,

this is only for u .

here is my code

class serDir

{

public static void main(String[] args)

{

String dirname="/java";

File f1=new File(dirname);

if(f1.isDirectory()){

System.out.println("directory name "+dirname);

String s[]=f1.list();

for(int i=0;i<s.lenght();i++){

File f=new(dirname +"/"+ s);

if(f.isDirectory()){

System.out.println("directory name "+s);

} else {

System.out.println("file name "+s);

}

}

} else {

System.out.println(dirname+"is not directory" );

}

}

}

but not working properly , now plz tell me what changes i should make to full fill my requirement . question is still same....................waiting>

kkaran007a at 2007-7-8 22:07:23 > top of Java-index,Java Essentials,Java Programming...
# 6

Right now your code basically did this:

if (f1 is directory) {

for each entry f2 under f1 {

if (f2 is directory) {

....

}

}

}

By the way, you should use [ code] ... [ /code] to

surround your code to make them easier to read

on the forum.

As you can see, you would need to duplicate the code

over and over again for the 3rd layer, 4th layer, 5th layer...

So that won't work.

You need recursion. Here is a rough pseudocode

(the function names may not be exact)

void searchForFile (File x) {

if (x.isDirectory()) {

for(String subfile:x.list()) {

searchForFile(x.getAbsolutePath() + File.separateChar + subfile); // Here, this is a recursive ("self") call

}

}

else if (x.isFile()) {

if x matches my criteria, then do something to x

}

}

Suntra_Vineeta at 2007-7-8 22:07:23 > top of Java-index,Java Essentials,Java Programming...
# 7
hi vineet ,thanks .thanks for other solutions and suggestion also .
kkaran007a at 2007-7-8 22:07:23 > top of Java-index,Java Essentials,Java Programming...