user3380152
user3380152

Reputation: 23

How to resolve a wildcard filespec using Java 7

I've searched, but can't seem to figure out how to write java code that takes as input a String containing a wildcard (asterisk), and outputs a String with the wildcard resolved.

I have a special situation where I know there is either 1 or 0 matching filespecs, so I'd like to have the returned String either be a valid filespec, or null.

I've gotten some example code to work using Files.walkFileTree(), but it doesn't do exactly what I want. I want to get the resollved filename back as a String that I can use in subsequent code...

I simply want to pass some code a String filename that includes an asterisk

e.g.: input this String:   filename*.tr

and get back a String with the asterisk resolved to the 1st matching filename (or null):

e.g.: get back this String:  filename_201402041230.tr

The directory where these files reside contains several thousand files, so iterating over all files in the directory and parsing the names myself isn't an attractive option.

Any help or pointers would be greatly appreciated.

Apologize for the outburst... Thanks for the tip... Here's what I was trying before: However, as I said this isn't what I want, but it's as close as I could get from my RESEARCH.

Path startDir = Paths.get("C:\\huge_dir");                                         
String pattern = "filename*.tr";                                                   

FileSystem fs = FileSystems.getDefault();                                          
final PathMatcher matcher = fs.getPathMatcher("glob:" + pattern);                  

FileVisitor<Path> matcherVisitor = new SimpleFileVisitor<Path>()                   
 {                                                                                 
 @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attribs)
  {                                                                                
  Path name = file.getFileName();                                                  
  if (matcher.matches(name))                                                       
    System.out.println(file);                                                      
  return FileVisitResult.TERMINATE;                                                
  }                                                                                
 };                                                                                
try                                                                                
  {                                                                                
  Files.walkFileTree(startDir, matcherVisitor);                                    
  }                                                                                
catch (Exception e){System.out.println(e);}                                        

Upvotes: 2

Views: 1749

Answers (1)

l4mpi
l4mpi

Reputation: 5149

You can use the nio2 Files.newDirectoryStream method with an additional pattern matcher to only list files which match the pattern. As your string already is a glob pattern, you can just pass it as the second argument:

String pattern = "filename*.tr"
try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir, pattern)) {
    //iterate over all matching files
    List<Path> paths = new ArrayList<>();
    for (Path path : ds) {
        paths.add(path);
    }
    if (paths.isEmpty()) {
        //no file found
    } else if (paths.size() == 1) {
        //found one result
        Path result = paths.get(0) //now do whatever
    } else {
        //more than one match - probably an error in your case?
    }
}

Upvotes: 1

Related Questions