[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

I want to find all files of a certain extension

Posted on 2006-06-20
6
Medium Priority
?
204 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 200 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 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.
Suggested Courses

656 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