This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes FilenameFilter help plz Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "FilenameFilter help plz" Watch "FilenameFilter help plz" New topic
Author

FilenameFilter help plz

kieran pattni
Ranch Hand

Joined: Jan 19, 2004
Posts: 47
please can someone show example of using overloaded method

public String[] list(FilenameFilter filter).

and tell me where i can learn by examples..

thank u advanced
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I often go to Google with "Java tutorial x" for some new topic. I almost always get a good hit at Sun and a bunch of university class notes.

FilenameFilter is an interface, so you'll have to write a class that implements FilenameFilter. That means you'll have to write the one method required by the interface. Are you comfortable writing a new little class from scratch and digging into the JavaDoc to learn about the method you need?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
kieran pattni
Ranch Hand

Joined: Jan 19, 2004
Posts: 47
hi stan...no i am not that comfortable using methods and looking at doc...to be honest..i am typing code from course text book most of the time..i have started looking more at doc..i get FilenameFilter interface but its method "public String[] list(FilenameFilter filter).

thank you..
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Some confusion.

The method definition for list() that you show is probably for the class File. By that I mean list() is one of the methods of the File class.
It can take an object of type FilenameFilter.
If when you use the list() method of File to select some files from a directory to be returned in the String[] list, you can use the FilenameFilter object can select or reject files.
You need to create a class that implements the FilenameFilter interface. Read the doc for the FilenameFilter interface. It requires that you code in the class implementing it to have one specific method. So in your class add that method and put the code in it to select files to be returned by the File.list() method.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It's hard to step back this far in a forum like this, much easier in person. But here we go ... start to make a little class like this:

"public class MyFilter" means you're writing a new class with that name. The simplest way to write a new class is to put it in a file with the same name, ie "MyFilter.java".

"implements FilenameFilter" means you're implementing an interface. You are required to write all the methods defined in the interface (or do some other more advanced stuff I'd like to ignore). The interface only has the one method "accept".

Back to the code you had before and fix it up to call this thing:

Now every time File.list() finds a file in the directory it will call your filter to ask if the file should go into the results. So far we coded the filter to always return true, so all files will go in just as if you had no filter. Let's make the filter more interesting:

Now it returns true for java files, false for all others.

Is this making sense? Are you in a class or working with someone to get started in Java? If not, maybe some of the gang can recommend good First Books.
kieran pattni
Ranch Hand

Joined: Jan 19, 2004
Posts: 47
thank you 4 your replies...i am doing home study...i have course material and java head first java....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FilenameFilter help plz