[ACCEPTED]-List of files starting with a particular letter in java-serialization

Accepted answer
Score: 20
File dir = new File(".");
if(!dir.isDirectory()) throw new IllegalStateException("wtf mate?");
for(File file : dir.listFiles()) {
    if(file.getName().startsWith("@"))
        process(file);
}

After revisiting this, it turns out there's 9 something else you can do. Notice the file 8 filter I used.

import java.io.File;
class Test {
    public static void main(String[] args) {
        File dir = new File(".");
        if(!dir.isDirectory()) throw new IllegalStateException("wtf mate?");
        for(File file : dir.listFiles(new RegexFileFilter("@*\\.ser"))) {
                process(file);
        }
    }

    public static void process(File f) {
        System.out.println(f.getAbsolutePath());
    }
}

Here's the RegexFileFilter 7 I used

public class RegexFileFilter implements java.io.FileFilter {

    final java.util.regex.Pattern pattern;

    public RegexFileFilter(String regex) {
        pattern = java.util.regex.Pattern.compile(regex);
    }

    public boolean accept(java.io.File f) {
        return pattern.matcher(f.getName()).find();
    }

}

And here's the result. Note the three 6 good files and the three bad files. If you 5 had to do this on a more regular basis, I'd 4 recommend using this, especially if you 3 need to do it based on other attributes 2 of the file other than file name, like length, modify 1 date, etc.

C:\junk\j>dir
 Volume in drive C has no label.
 Volume Serial Number is 48FA-B715

 Directory of C:\junk\j

02/14/2012  06:16 PM    <DIR>          .
02/14/2012  06:16 PM    <DIR>          ..
02/14/2012  06:15 PM                 0 @bad.serr
02/14/2012  06:15 PM                 0 @badser
02/14/2012  06:15 PM                 0 @first.ser
02/14/2012  06:15 PM                 0 @second.ser
02/14/2012  06:15 PM                 0 @third.ser
02/14/2012  06:15 PM                 0 bad@file.ser
02/14/2012  06:24 PM               692 RegexFileFilter.class
02/14/2012  06:24 PM               338 RegexFileFilter.java
02/14/2012  06:24 PM               901 Test.class
02/14/2012  06:24 PM               421 Test.java
              10 File(s)          2,352 bytes
               2 Dir(s)  10,895,474,688 bytes free

C:\junk\j>java Test
@first.ser
@second.ser
@third.ser
Score: 4

If it helps check java.io.FileFilter .

0

Score: 2

You could use java.nio.file.DirectoryStream;

import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

  //Java version 7 
  class A { 
    public static void main(String[] args) throws Exception
    { 
       // Example directory on dos system
       Path dir = Paths.get("c:\\a\\b\\");

        /**
        *
        * Create a new DirectoryStream for the above path. 
        * 
        * List all files within this directory that begin
        * with the letters A or B i.e "[AB)]*"
        * 
        */
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "[AB]*"))
        {
            // Print all the files to output stream
            for(Path p: stream)
            {  
                System.out.println(p.getFileName());
            }
        }
        catch(Exception e)
        {
            System.out.println("problems locating directory");
        }
    }
 } 

0

Score: 1

Yes, open a directory File, get its List 3 of child Files using a FileFilter that only 2 allows through those file names that you 1 want.

Score: 0

please see here, I think that is what you need. See 1 the post referring to FileFilter

More Related questions