I want to find all files of a certain extension

Posted on 2006-06-20
Last Modified: 2010-03-31
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.
Question by:arctanx
  • 2
  • 2
  • 2
LVL 86

Assisted Solution

CEHJ earned 50 total points
ID: 16946261
LVL 14

Expert Comment

ID: 16946293
String[] all = new File("dir").list();
ArrayList<String> al = new ArrayList<String>();
for(String name : al)
String[] files = al.toArray(new String[0]);

Author Comment

ID: 16946600
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.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

LVL 14

Accepted Solution

StillUnAware earned 50 total points
ID: 16946642
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) {
      return true;
    return false;

Author Comment

ID: 16946691
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.
LVL 86

Expert Comment

ID: 16946716

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

will do in fact

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
varialbe initialization 11 37
MySqlDump not dumping triggers 1 43
maven module vs maven project 3 46
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question