Solved

File Filter Question

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

738 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