How to open all files that starts with specific prefix in java?

http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#listFiles(java.io.FilenameFilter) use this method on the parent folder and implements the FilenameFilter like:

boolean accept(File dir, String name){
     return name.matches("AB-\n{2}.*")
}

Yes. Use File.listFiles(FilenameFilter):

As an example:

File dir = new File("/path/to/directory");
File[] foundFiles = dir.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.startsWith("Ab-");
    }
});

for (File file : foundFiles) {
    // Process file
}    

Of course, change the condition in the accept() method to whatever you need. So maybe name.startsWith("Ab-") && name.endsWith(".txt").

Tags:

Java

File