Two Laptop Bag*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "To fetch files from a dir" Watch "To fetch files from a dir" New topic
Author

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
Chhaya
John Zukowski
Author
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="http://www.amazon.com/exec/obidos/ASIN/189311578X/ref=ase_electricporkchop/107-7882751-0234939" target="_blank" rel="nofollow">"Definitive Guide to Swing for Java 2"</a>,<br /><a href="http://www.amazon.com/exec/obidos/ASIN/1893115925/ref%3Dase%5Felectricporkchop/102-5437230-7785719" target="_blank" rel="nofollow">"Java Collections"</a> and <a href="http://www.amazon.com/exec/obidos/ASIN/1893115984/ref%3Dase%5Felectricporkchop/102-5437230-7785719" 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
thanx
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:

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: To fetch files from a dir
 
Similar Threads
JList Refresh
How to set a custom icon for a specific file type?
text data files
How to use JDO
Apply FilenameFilter to only 1 file