Solved

I want to find all files of a certain extension

Posted on 2006-06-20
6
202 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

688 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