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
Solved

File Filter Question

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

839 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