The reason for this is that it does not see the second one (util)
as existing. But it saw jat as existing.
The code for the class extension may be found below. For the
directories mentioned above, I am getting the following output:
First level down:
***** Directory: C:\WINDOWS\Profiles\Alan000\My Documents\My Java Code
Checking file acceptance criteria for jat
name = null
extension = java
includeDirectories = true
jat is a directory.
jat exists.
Value of fileOK = true
Next level down:
***** Directory: C:\WINDOWS\Profiles\Alan000\My Documents\My Java Code
\jat
Checking file acceptance criteria for util
name = null
extension = java
includeDirectories = true
util is NOT a directory!!!
util does NOT exist!!!
Value of fileOK = false
Is it obvious to anyone what I am doing wrong? How can one
directory be recognized while another is not?
Thanks, Alan
class FileListFilter implements FilenameFilter
{
private String name, extension;
private boolean includeDirectories;
public FileListFilter(String name, String extension,
boolean includeDirectories)
{
this.name = name;
this.extension = extension;
this.includeDirectories = includeDirectories;
}
public boolean accept(File directory, String filename)
{
boolean fileOK = true;
System.out.println("***** Directory: " + directory);
System.out.println("Checking file acceptance criteria for "
+ filename);
System.out.println(" name = " + name);
System.out.println(" extension = " + extension);
System.out.println(" includeDirectories = "
+ includeDirectories);
if (name != null)
{ fileOK &= filename.startsWith(name); }
if (extension != null)
{
if (fileOK)
{
fileOK &= filename.endsWith('.' + extension);
if (!fileOK && includeDirectories)
{
File file = new File(filename);
if (file.isDirectory())
{ System.out.println(file.getName() + " is a
directory."); }
else
{ System.out.println(file.getName() + " is NOT a
directory!!!"); }
if (file.exists())
{ System.out.println(file.getName() + " exists."); }
else
{ System.out.println(file.getName() + " does NOT
exist!!!"); }
fileOK = file.isDirectory();
}
}
}
System.out.println("Value of fileOK = " + fileOK);
return fileOK;
}
}
"wrapped lines do NOT
compile!!!"
Beware line-wrap in code posted to usenet. I
recommend limiting code listing lines to under
62 chars (though do not lose all indentation to
achieve that!).
Here is a tool to help check line width.
<http://www.physci.org/twc.jnlp>
--
Andrew Thompson
http://www.physci.org/
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
Does recursion and me creating a file object in a recursive
method possibly cause the problem?
Thanks, Alan
public static List<File> listAllFiles(File dir, String extension)
throws Exception
{
if (dir.exists())
{ System.out.println(".....directory " + dir.getName()
+ " exists."); }
else
{ System.out.println(".....directory " + dir.getName()
+ " does NOT exist!!!"); }
try
{
if (!dir.isDirectory())
{
System.out.println("******* Not a directory, returning
null *********");
return null; // Not a directory
}
else // input file reference is a directory
{
// Initialize lists of files and directories
List<File> FileAndDirList = new ArrayList<File> ();
List<File> filelist = new ArrayList<File> ();
// Get list of files in the input directory
FilenameFilter select = new FileListFilter(null,
extension, true);
File[] filearray = dir.listFiles(select);
System.out.println("Length of array = "
+ filearray.length);
// If file array is not empty, convert it to a list
if (filearray.length > 0)
{
FileAndDirList = Arrays.asList(filearray);
System.out.println("\nFiles in directory:");
for (Iterator<File> pit = FileAndDirList.iterator();
pit.hasNext();)
{
File afile = pit.next();
System.out.println(afile.getName());
} System.out.println("\n");
}
// If there are files and/or directories, check
// to see if they are directories or other files
if (!FileAndDirList.isEmpty())
{
boolean addedOK;
for (Iterator<File> it = FileAndDirList.iterator();
it.hasNext();)
{
File file = new File(it.next().getName());
if (!file.isDirectory())
{
addedOK = filelist.add(file);
}
else
{
System.out.println("Directory: "
+ file.getName());
List<File> temp = new ArrayList<File> ();
temp = listAllFiles(file, extension);
if (temp != null)
{
addedOK = filelist.addAll(temp);
}
}
}
}
return filelist;
}
}
catch ( SecurityException e )
{
e.printStackTrace();
return null;
}
catch ( RuntimeException e )
{
e.printStackTrace();
return null;
}
}
}
Not the problem I was referring to, and if you think
it did*, it serves to convince me that I utterly (, utterly,
utterly) failed to communicate my message.
I'll try again, a different way, with more words.
When I copy/pasted that original source (the same
thing would happen for the new source) into my
editor/compiler and told it to 'compile' the code posted,
it pointed out a number of errors, including..
D:\projects\FileListFilter.java:39: unclosed string literal
{ System.out.println(file.getName() + " is a
^
Try it yourself, from the Google representation**, or your
own news reader.
**
<http://groups.google.com/group/comp.lang.java.programmer/msg/17b121d410a2c49d
>
If other people can compile the code, and see
the problem for themselves, 'at runtime', code
becomes a lot easier to debug. For that reason,
I recommend posting an SSCCE.
<http://www.physci.org/codes/sscce.html>
There are a number of things that need to 'work'
for code to be considered an SSCCE, including
that an SSCCE that tries to demonstrate a
runtime problem, should compile cleanly.
Code with wrapped lines does not compile cleanly.
<http://www.physci.org/codes/sscce.html#linewidth>
Te help check that line width is less than the width
that will be wrapped, I provide the Text Width Checker.
<http://www.physci.org/twc.jnlp>
Note there are a number of other tips in the SSCCE
document about how to go about creating an SSCCE,
please read it, as the code is lacking in other respects
detailed in the document, and I could not be bothered
adding those bits myself.
* So. After having gone 'the long way around', I'd like
to know ..
1) If you clicked the link to the TWC earlier?
2) What happened when you did? and
3) Why you might have thought that related to file
name filters and recursion?
You can use them, or start with ones that work and add your own
wrinkles, or just study them to see how the work.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Can we see the code for FilenameFilter?
How are files and directories represented in Java?
You can download the code from Sun, but it won't be much because it's an
interface. Better would be to read the Javadocs for it:
<http://java.sun.com/javase/6/docs/api/java/io/FilenameFilter.html>
> How are files and directories represented in Java?
By whether File.isDirectory() returns true.
<http://java.sun.com/javase/6/docs/api/java/io/File.html#isDirectory()>
or contrariwise, whether isFile() does not.
<http://java.sun.com/javase/6/docs/api/java/io/File.html#isFile()>
Needless to say the File methods are pass-throughs for information from the
underlying OS.
To the OP: You are truly going to regret all those println() calls in a
FilenameFilter.accept() method. They obscure the logic, both at compile time
and run time, and will slow things down horribly. What you want is the
'assert' keyword.
A FilenameFilter is supposed to be rather lean, implementable as an anonymous
class. Sometimes it pays to write one into fullness, as you did, for the more
complex implementations, which perhaps this is. Even so, keep the sensibility
of a lean implementation when writing one in its own named class.
Given the kinds of tests you're performing, you might consider implementing a
FileFilter instead, in conjunction with the File.listFiles( FileFilter ) method.
--
Lew
Thanks, Alan
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. There`s hardly any way for someone to quickly take a
look at what I initially posted. In addition, since I`ve made no
progress, there`s no good alternatives, anyway.
As for the recursion, this was just something else which I thought
might be involved in the problem. However, I made a couple of small
programs that shows that recursion itself is not the problem. I
thought that, perhaps, it had something to do with me creating new
File types in the recursion. You may find them below, hopefully in a
usable format. However, in retrospect, I don`t know why you would
want them.
Thank you, Alan
import java.io.*;
import java.util.*;
public class TryRecursion
{
public static void main(String[] args)
{
File afile = new File("appClass.class");
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 afile = new File("appClass2.class");
recurs(afile);
}
}
import java.io.*;
import java.util.*;
public class Bonehead
{
public static void main(String[] args)
{
File file1 = new File("appClass.class");
if (file1.exists())
{ System.out.println("file1 exists"); }
else
{ System.out.println("file1 does NOT exist!!!"); }
File file2 = new File("appClass2.class");
if (file2.exists())
{ System.out.println("file2 exists"); }
else
{ System.out.println("file2 does NOT exist!!!"); }
File file3 = new File("appClass.class");
if (file3.exists())
{ System.out.println("file3 exists"); }
else
{ System.out.println("file3 does NOT exist!!!"); }
}
}
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