Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FilenameFilter not working???  RSS feed

 
Ian Montgomery
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all.
Trying to get handle on the FilenameFilter interface and not having much luck. I've implemented the following in an anonymous inner class and couldn't get it to work (the way I thought it should) so I put it into the two classes listed. I've tried hard coding the file extension too without any luck. What gives? If I pass an empty string, I get the entire listing for the given directory. If I pass ".txt" or whatever it comes back with nothing.
import java.io.FilenameFilter;
import java.io.File;
public class RenameFilter implements FilenameFilter
{
String ext;

public RenameFilter( String e)
{
ext = e;
}
public boolean accept( File f, String s )
{
return f.getName().toLowerCase.endsWith( ext );
}

}
*************************
import java.io.*;
public class RenameDelimFile
{
public static void main( String[] args )
{
String path = args[ 0 ];

String filter = args[ 1 ];
File file = new File( path );
String[] files = file.list( new RenameFilter( filter ) );

for ( int i = 0; i < files.length; i++ )
{
System.out.println( ( i + 1 ) + " " + files[ i ] );
}
System.exit( 0 );
}
//end main
}
Thanks,
--Ian
 
Paul Lester
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The signature for FilenameFilter.accept() is
public boolean accept(File dir, String name)
Instead of doing:

You are checking the directory name instead of the file name.
Paul
 
Ian Montgomery
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, the toLowerCase, really does have the parentheses. Sorry.
 
Ian Montgomery
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Paul. That did it!
This was a very confusing interface for some reason. I'm still not sure I make the connection between how the filter knows what's the file and what's the path. You just apply the filter and it works!
Anyway, thanks a bunch for your quick and accurate response.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!