I want to find all files of a certain extension

I've got my zip utility working.  But I'm hardcoding the file names, like:

String[] files = new String[2];

files[0] = "C:\\Temp\\file1.txt";
files[1] = "C:\\Temp\\file2.txt";

I want to loop through a given dir, like C:\Temp, and pick up all files with a certain extension and put them in an array.

All the examples on the web aren't really what I want.

I'd like to keep this to a simple method if I can.
LVL 2
arctanxAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
StillUnAwareConnect With a Mentor Commented:
Actually, You should use the technique, mentioned by CEHJ:

String[] files = new File("C:\\Temp").list(new FilenameFilter() {
  public boolean accept(File dir, String name) {
    if(name.toUpperCase().endsWith(".TXT"))
      return true;
    return false;
  }
});
0
 
StillUnAwareCommented:
String[] all = new File("dir").list();
ArrayList<String> al = new ArrayList<String>();
for(String name : al)
    if(name.toUpperCase().endsWith(".TXT"))
        al.add(name);
String[] files = al.toArray(new String[0]);
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
arctanxAuthor Commented:
So this is what I've got so far:

        String dir = "C:\\Temp";
        File path = new File(dir);
        String[] list = path.list();
        for(int i=0;i<list.length;i++)
        {
            f.listFiles( WHAT GOES HERE );
        }

How exactly do I use FileFilter?

StillUnAware I can't use that 1.5 code we're using 1.4.  Thanks though.
0
 
arctanxAuthor Commented:
That's gravy!!
I'd like to split points, I going to use your code, but CEHJ had the idea.
I hope 50/50 is cool.
0
 
CEHJCommented:
:-)

return name.toUpperCase().endsWith(".TXT");

will do in fact
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.