• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

FileFilter?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai! everybody

this prog.. is working with the listing of the ".java" files in current directory ooops!.. but i want the proper expalnation. for this
String[] str = curdir.list(new JavaFilter());
statement execution.
i want specifically how JavaFilter class is created & invoking the accept() method.


import java.io.*;

class JavaFilter extends Object implements FilenameFilter{
public JavaFilter(){}
public boolean accept (File dir ,String name){

return name.endsWith(".java");
}
}
public class ListJava extends JavaFilter {
public static void main(String args[]){
File curdir = new File(".");
String[] str = curdir.list(new JavaFilter());

for(int i = 0; i<str.length; i++)>
System.out.println(str[i]);
}

}
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hmmm....I should say this is a good example.
a good learning for me too. However I don't think
the real exam may not need such detailed knowledge...IMO.
Coming to your qstn, this is what the J2 API has to say...

public String[] list(FilenameFilter filter)
Returns an array of strings naming the files and directories
in the directory denoted by this abstract pathname that satisfy
the specified filter. The behavior of this method is the
same as that of the list() method, except that the strings in
the returned array must satisfy the filter. If the given filter
is null then all names are accepted. Otherwise, a name satisfies
the filter if and only if the value true results when the
FilenameFilter.accept(java.io.File, java.lang.String) method
of the filter is invoked on this abstract pathname and the name of a file or directory in the
directory that it denotes.
Parameters:
filter - A filename filter
Returns:
An array of strings naming the files and directories in the directory denoted by this abstract pathname that were accepted
by the given filter. The array will be empty if the directory
is empty or if no names were accepted by the filter. Returns
null if this abstract pathname does not denote a directory, or
if an I/O error occurs.
Throws:
SecurityException - If a security manager exists and its
SecurityManager.checkRead(java.io.FileDescriptor) method
denies read access to the directory

and re the FilenameFilter interface:

public interface FilenameFilter
Instances of classes that implement this interface are used to filter filenames. These instances are used to filter directory listings in the list method of class File, and by the Abstract
Window Toolkit's file dialog component.

This interface has only one method called accept() and you code
implements this method. Details of the method asin the API:

accept
public boolean accept(File dir, String name)
Tests if a specified file should be included in a file list.
Parameters:
dir - the directory in which the file was found.
name - the name of the file.
Returns:
true if and only if the name should be included in the file list; false otherwise.

In case you are wondering abt the API, its here.
Also, the code is not very clear above, especially the for loop,
should read:

Regds.
- satya
 
Hot dog! An advertiser loves us THIS much:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic