Re: FilenameFilter woes
Alan wrote:
...
I did not see, when I initially read your post, how it was
helpful. However, now I do. I will make a shorter example, which
makes sense.
Those two sources were sure easier to work with.
I can see why the first class got 'stuck in a loop',
since after the first call, it was always calling itself
with the same file, that in no clear way related to
the initial argument!
In any case, this (below) is an SSCCE of code
that recursively lists the directories and files in
the parent of the directory in which the class
resides.
<sscce>
import java.io.*;
import java.util.*;
public class TryRecursion
{
public static void main(String[] args) throws Exception
{
File aFile = new File(".", "TryRecursion.class");
for (int ii=0; ii<2; ii++ )
{
if (aFile.getParentFile()!=null)
{
aFile = new File(
aFile.getCanonicalPath()).getParentFile();
System.out.println("parent " + aFile);
}
}
System.out.println(
"Search ancestor directory. " + aFile );
recurs(aFile);
}
public static void recurs ( File file )
{
if (file.exists())
{
System.out.println(
file.getName() + " exists");
}
else
{
System.out.println(
file.getName() + " does NOT exist!!!");
}
File[] files = file.listFiles();
for (int ii=0; ii<files.length; ii++)
{
if (files[ii].isDirectory())
{
System.out.println( "dir: " + files[ii] );
recurs(files[ii]);
}
else
{
System.out.println(files[ii]);
}
}
}
}
</sscce>
HTH
--
Andrew Thompson
http://www.physci.org/
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1