File io command to list dir with wildcards


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: File io command to list dir with wildcards

  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.

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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center