Solved

File Filter Question

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 79
spring jars download 1 26
null output 3 24
login form jsp example 2 24
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

785 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