Solved

I want to find all files of a certain extension

Posted on 2006-06-20
6
201 Views
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.
0
Comment
Question by:arctanx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 16946261
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16946293
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
 
LVL 2

Author Comment

by:arctanx
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
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) {
    if(name.toUpperCase().endsWith(".TXT"))
      return true;
    return false;
  }
});
0
 
LVL 2

Author Comment

by:arctanx
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16946716
:-)

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

will do in fact
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

730 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