Solved

File Filter Question

Posted on 2002-06-20
4
160 Views
Last Modified: 2010-03-31
Hi i'd like to get a list of all xml files in a given directory and read them in a variable - quite simple really, just one problem i'm a java newbie!!

here's my method so far:

public static void check_for_upd() {
       
        File t = new File("/home/archive/ibar/sid/external");
        File[]  my_list = t.listFiles();
       
        String filter = "xml";
       
                         
        String my_path = t.getPath();
                         
        System.out.println("will check for a upd within path: " + my_path);
       
        for (int i = 0; i < my_list.length; i++) {
           
            System.out.println("we have found a file:" + my_list[i].getName());
           
        }
       
    }

help would be much appreciated and some reference material.

many thanks
Peewee
0
Comment
Question by:Peewee
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 7096678
You can filter the names that the listFiles method gets by passing it a FileNameFilter. FileNameFilter is an interface defined in the io package, that contains one method:

public abstract boolean accept(File dir,String name);

So for your case you could write a small class along the following lines:

class XMLFilter implements FilenameFilter
{   public boolean accept(File dir,String name)
    {   return(name.endsWith(".xml"));
    }
}

Which would then be passed in your listFiles call:

       File t = new File("/home/archive/ibar/sid/external");
       XMLFilter XF=new XMLFilter();
       File[]  my_list = t.listFiles(XF);
       

       
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7096711
just add a file filter to your listFiles():

File[] my_list = t.listFiles(
     new FileFilter() {
          public boolean accept(File f) {
             String fileName = f.getName();
             String ext = fileName.substring(f.lastIndexOf("."));
       
             if (ext.equals(filter)) {
                 return true;
             } else {
                 return false;
             }
          }
     }
);
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7096718
actually:
File[] my_list = t.listFiles(
    new FileFilter() {
         public boolean accept(File f) {
            return f.getName().endsWith(filter);
         }
    }
);

0
 
LVL 16

Expert Comment

by:imladris
ID: 7096730
As for reference material, you could use the Sun java site. http://java.sun.com provides documentation for java stuff. The 1.2 version is at:

http://java.sun.com/products/jdk/1.2/docs/api/index.html

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now