Solved

File Filter Question

Posted on 2002-06-20
4
170 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
[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
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

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!

Question has a verified solution.

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

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

756 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