DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2003
    Posts
    2

    File io command to list dir with wildcards

    The below code gives a NullPointerException when i try to include wildcards on the File command. If i leave off the \\*.zip it will work fine. How can i filter this command?

    import java.io.*;
    ...
    String dir = "C:\\parentdir\\subdir\\*.zip";
    ...
    // Get list of files in a give directory
    File fname = new File(dir);
    String[] file_list = fname.list();
    The Truth will set you free

  2. #2
    Join Date
    Feb 2003
    Posts
    2

    Post

    "I" figured it out...

    import java.io.*;

    public class DirList {
    public static void main(String[] args) {
    try {
    File path = new File(".");
    String[] list;
    if(args.length == 0)
    list = path.list();
    else
    list = path.list(new DirFilter(args[0]));
    for(int i = 0; i < list.length; i++)
    System.out.println(list[i]);
    } catch(Exception e) {
    e.printStackTrace();
    }
    }
    }

    class DirFilter implements FilenameFilter {
    String afn;
    DirFilter(String afn) { this.afn = afn; }
    public boolean accept(File dir, String name) {
    // Strip path information:
    String f = new File(name).getName();
    return f.indexOf(afn) != -1;
    }
    }
    The Truth will set you free

  3. #3
    Join Date
    Nov 2002
    Posts
    138
    yep you won't get anyting if you use *. you could use the endsWith() method since you are manipulating strings to get specific types of files. or you could use the pattern class in jdk1.4++. i haven't used this yet, but supposedly it handles wildcards. your solution is fine.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links