Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > FilenameFilter woes

Reply
Thread Tools

FilenameFilter woes

 
 
Alan
Guest
Posts: n/a
 
      12-29-2007
I have extended the FilenameFilter class, and it works in finding
files in the current directory ("My Java Code\") and its next level
down subdirectories (e.g., "My Java Code\jat). However, when it gets
below that level (e.g., at "My Java Code\jat\util"), it no longer
recognizes a subdirectory as being a directory.

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;
}
}
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      12-29-2007
Alan wrote:
...
> { System.out.println(file.getName() + " is NOT a
>directory!!!"); }


"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.asp...neral/200712/1

 
Reply With Quote
 
 
 
 
Alan
Guest
Posts: n/a
 
      12-29-2007
Maybe the problem is in the code that uses the FilenameFilter
class. It uses recursion. The failure does not come on the first
invocation but on the first recursive call. The calling method is
shown below.

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;
}
}
}


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      12-29-2007
Alan wrote:
...
> Does recursion and me creating a file object in a recursive
>method possibly cause the problem?


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....b121d410a2c49d
>


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?

--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200712/1

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-29-2007
I have written a library of filters . See
http://mindprod.com/products1.html#FILTER

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
 
Reply With Quote
 
Charles
Guest
Posts: n/a
 
      12-29-2007
On Dec 28, 8:35*pm, Alan <(E-Mail Removed)> wrote:
> * *I have extended the FilenameFilter class, and it works in finding
> files in the current directory ("My Java Code\") and its next level
> down subdirectories (e.g., "My Java Code\jat). *However, when it gets
> below that level (e.g., at "My Java Code\jat\util"), it no longer
> recognizes a subdirectory as being a directory.
>
> * * 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;
> * * * }
> * *}


 
Reply With Quote
 
Charles
Guest
Posts: n/a
 
      12-29-2007
On Dec 28, 8:35*pm, Alan <(E-Mail Removed)> wrote:
> * *I have extended the FilenameFilter class, and it works in finding
> files in the current directory ("My Java Code\") and its next level
> down subdirectories (e.g., "My Java Code\jat). *However, when it gets
> below that level (e.g., at "My Java Code\jat\util"), it no longer
> recognizes a subdirectory as being a directory.
>
> * * 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;
> * * * }
> * *}


Can we see the code for FilenameFilter?

How are files and directories represented in Java?


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-29-2007
Charles wrote:
> Can we see the code for FilenameFilter?


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
 
Reply With Quote
 
Alan
Guest
Posts: n/a
 
      12-29-2007
Lew,
The reason there are any println statements at all is because I
am going nuts trying to figure out why the heck this error is
occurring.

Thanks, Alan


 
Reply With Quote
 
Alan
Guest
Posts: n/a
 
      12-29-2007
Andrew,

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!!!"); }
}

}



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i modify this program to list the *.java file using FIlenameFilter mahesh Java 3 12-06-2006 11:06 AM
how can i modify this program to list the *.java file using FIlenameFilter mahesh Java 0 12-06-2006 05:14 AM
FilenameFilter details Bill Medland Java 1 03-03-2006 11:11 AM
adhoc network 11g wireless dropout woes dkh Wireless Networking 1 08-02-2004 01:27 PM
Re: Easy FileFiletr/FilenameFilter Andy Flowers Java 0 06-29-2003 08:54 AM



Advertisments