A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Java
»
Java in General
Author
Reading dir
Manuel Paco
Ranch Hand
Joined: Sep 23, 2001
Posts: 58
posted
Jun 08, 2002 09:50:00
0
Help please
How can I get all let's say "gif" or "jpg" files residing in the directory
"C:\MyFiles" to my
String
array.
Mark Herschberg
Sheriff
Joined: Dec 04, 2000
Posts: 6037
posted
Jun 09, 2002 16:49:00
0
Use the
java.io.File
's
list()
and related methods. If you want to get complex, use the
java.io.FilenameFilter
interface. In any case, you'll end up with an array of Strings.
--Mark
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
posted
Jun 09, 2002 20:09:00
0
class MyFilenameFilter implements java.io.FilenameFilter { public boolean accept( java.io.File dir , String name ) { return ( name.endsWith( ".jpg" ) || name.endsWith( ".gif" ) ); } }
import java.io.File ; public class Whatever { public static void main( String[] args ) throws Exception { File directory = new File( "c:\\MyFiles" ); String[] images = directory.list( new MyFilenameFilter() ); for ( int i = 0 ; i < images.length ; i++ ) { System.out.println( images[ i ] ); } } }
[
How To Ask Good Questions
] [
JavaRanch FAQ Wiki
] [
JavaRanch Radio
]
Manuel Paco
Ranch Hand
Joined: Sep 23, 2001
Posts: 58
posted
Jun 09, 2002 21:53:00
0
Thank you very much
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: Reading dir
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter