File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes To fetch files from a dir Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "To fetch files from a dir" Watch "To fetch files from a dir" New topic

To fetch files from a dir

Chhaya Dhanani
Ranch Hand

Joined: Apr 11, 2001
Posts: 70
Hi all, this comes as an SOS
I am developing an application using Swings
I have to pick up all files with extension .dat from say a dir called "c:\chhaya".
how do i go about doing this.
I should show thw total number of files with .dat extension and then populate a JList which allows multiple selection with those file names.
populating the JList is no problem.
how do i get or search for files with a specific extension in a specific dir.
Thannx in advance
Please reply as soon as possible
John Zukowski
Ranch Hand

Joined: Mar 29, 2001
Posts: 88
Look at the listFiles() methods of File. You can pass in a filter to say what type of files you want.
John Zukowski Author of "Definitive Guide to Swing for Java 2" and "Java Collections"

John Zukowski Author of <a href="" target="_blank" rel="nofollow">"Definitive Guide to Swing for Java 2"</a>,<br /><a href="" target="_blank" rel="nofollow">"Java Collections"</a> and <a href="" target="_blank" rel="nofollow">"Learn Java with JBuilder 6"</a>
Chhaya Dhanani
Ranch Hand

Joined: Apr 11, 2001
Posts: 70
Thanks John,
I went thru' the API
but am still confused as to how i should go about implemneting FilenameFilter interface
a code snippet from you would definitely help
PS. please reply today itself, if possible
Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
There is only one method in FilenameFilter that needs to be implemented. Here is a filter that accepts only JSP files:

I agree. Here's the link:
subject: To fetch files from a dir
jQuery in Action, 3rd edition