Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursion and files

Status
Not open for further replies.

ztopher

Technical User
Aug 4, 2002
6
SE
Hello

I need some help with a recursive function that is supposed to read dirs from an arbitrary location from the file system. When I call the function the second time from within the function no subdirs are found, I don’t think the file filter is called. Please help me out. My code looks like this:

Thank you!


public void buildStructure(File filename, FileObject parent)
{
this.parent = parent;
this.workDir = filename;
System.out.println("workDir is " + filename);
FileFilter fileFilter = new FileFilter()
{
public boolean accept(File file)
{
boolean b = file.isDirectory();
System.out.println("isdir " + b);
return b;
}
};
/*Check if there is any subdirs in dir <filename>*/
File[] subDirs = workDir.listFiles(fileFilter);
FileObject fob = new FileObjectImpl(workDir, parent);
if (subDirs == null)
{
System.out.println(&quot;Subdirs is null&quot;);
}
else
{
System.out.println(&quot;subDirs length &quot; + subDirs.length);
for (int i = 0; i < subDirs.length; i++)
{
System.out.println(&quot;addsub dir &quot; + subDirs);
fob.addSubDir(new FileObjectImpl(subDirs,fob));
buildStructure(subDirs,fob);
}
}

}
 
I removed the references to FileObjects (because I don't know what they are) in the code and the rest of it ran it just fine.

Are you absolutely sure there ARE subdirectories beneath the one you start with?

Here's the test program I tried:

Code:
import java.io.*;

public class Class1 
{
   File workDir = null;
   
   public Class1()
   {
      buildStructure( new File(&quot;c:\\test&quot;));
   }

   public static void main(String[] args)
   {
      Class1 class1 = new Class1();
   }


   public void buildStructure(File filename)
   {
   
        this.workDir = filename;
        System.out.println(&quot;workDir is &quot; + filename);
        FileFilter fileFilter = new FileFilter()
        {
            public boolean accept(File file)
            {
                boolean b = file.isDirectory();
               // System.out.println(&quot;isdir &quot; + b);
                return b;
            }
        };
        /*Check if there is any subdirs in dir <filename>*/
        File[] subDirs = workDir.listFiles(fileFilter);
        if (subDirs == null)
        {
            System.out.println(&quot;Subdirs is null&quot;);
        }
        else
        {
         //   System.out.println(&quot;subDirs length &quot; + subDirs.length);
            for (int i = 0; i < subDirs.length; i++)
            {
                System.out.println(&quot;addsub dir &quot; + subDirs[i]);
                buildStructure(subDirs[i]);
            }
        }
   }   
    
}

 
Thank you!

When I run this code from a GUI, using JFileChooser to select start dir it does not run properly. When the function is called a second time no subdirs are found. Even though I know that sub dirs exist. When I use a test stub without the GUI i works OK.

Very Strange, Maybe it is some issue with the file filter? When I replaced that part from my code it works fine =)



Cheers
Ztopher


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top