4 import java.io.IOException;
5 import java.nio.file.FileSystems;
6 import java.nio.file.FileVisitOption;
7 import java.nio.file.FileVisitResult;
8 import java.nio.file.Files;
9 import java.nio.file.Path;
10 import java.nio.file.PathMatcher;
11 import java.nio.file.Paths;
12 import java.nio.file.SimpleFileVisitor;
13 import java.nio.file.attribute.BasicFileAttributes;
14 import java.util.ArrayList;
15 import java.util.EnumSet;
16 import java.util.List;
17 import java.util.stream.Collectors;
19 public class FileUtils
22 * Given string glob pattern (see
23 * https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystem.html#getPathMatcher(java.lang.String)
24 * ) return a List of Files that match the pattern.
25 * Note this is a java style glob, not necessarily a bash-style glob, though there are sufficient similarities.
27 public static List<File> getFilesFromGlob(String pattern)
29 return getFilesFromGlob(pattern, true);
32 public static List<File> getFilesFromGlob(String pattern,
33 boolean allowSingleFilenameThatDoesNotExist)
35 pattern = substituteHomeDir(pattern);
36 List<File> files = new ArrayList<>();
38 * For efficiency of the Files.walkFileTree(), let's find the longest path that doesn't need globbing.
39 * We look for the first glob character * { ? and then look for the last File.separator before that.
40 * Then we can reset the path to look at and shorten the globbing pattern.
41 * Relative paths can be used in pattern, which work from the pwd (though these are converted into
42 * full paths in the match).
44 int firstGlobChar = -1;
45 boolean foundGlobChar = false;
46 for (char c : new char[] { '*', '{', '?' })
48 if (pattern.indexOf(c) > -1
49 && (pattern.indexOf(c) < firstGlobChar || !foundGlobChar))
51 firstGlobChar = pattern.indexOf(c);
55 int lastFS = pattern.lastIndexOf(File.separatorChar, firstGlobChar);
58 String pS = pattern.substring(0, lastFS + 1);
59 String rest = pattern.substring(lastFS + 1);
60 Path parentDir = Paths.get(pS).toAbsolutePath();
61 if (parentDir.toFile().exists())
65 String glob = "glob:" + parentDir.toString() + File.separator
67 PathMatcher pm = FileSystems.getDefault().getPathMatcher(glob);
68 int maxDepth = rest.contains("**") ? Integer.MAX_VALUE
70 .filter(ch -> ch == File.separatorChar).count())
72 Files.walkFileTree(parentDir,
73 EnumSet.of(FileVisitOption.FOLLOW_LINKS), maxDepth,
74 new SimpleFileVisitor<Path>()
77 public FileVisitResult visitFile(Path path,
78 BasicFileAttributes attrs) throws IOException
82 files.add(path.toFile());
84 return FileVisitResult.CONTINUE;
88 public FileVisitResult visitFileFailed(Path file,
89 IOException exc) throws IOException
91 return FileVisitResult.CONTINUE;
94 } catch (IOException e)
103 File f = new File(pattern);
104 if (allowSingleFilenameThatDoesNotExist || f.exists())
113 public static List<String> getFilenamesFromGlob(String pattern)
115 // convert list of Files to list of File.getPath() Strings
116 return getFilesFromGlob(pattern).stream().map(f -> f.getPath())
117 .collect(Collectors.toList());
120 public static String substituteHomeDir(String path)
122 return path.startsWith("~" + File.separator)
123 ? System.getProperty("user.home") + path.substring(1)