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.Collections;
16 import java.util.EnumSet;
17 import java.util.List;
18 import java.util.stream.Collectors;
20 import jalview.bin.Console;
22 public class FileUtils
25 * Given string glob pattern (see
26 * https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystem.html#getPathMatcher(java.lang.String)
27 * ) return a List of Files that match the pattern.
28 * Note this is a java style glob, not necessarily a bash-style glob, though there are sufficient similarities.
30 public static List<File> getFilesFromGlob(String pattern)
32 return getFilesFromGlob(pattern, true);
35 public static List<File> getFilesFromGlob(String pattern,
36 boolean allowSingleFilenameThatDoesNotExist)
38 pattern = substituteHomeDir(pattern);
39 List<File> files = new ArrayList<>();
41 * For efficiency of the Files.walkFileTree(), let's find the longest path that doesn't need globbing.
42 * We look for the first glob character * { ? and then look for the last File.separator before that.
43 * Then we can reset the path to look at and shorten the globbing pattern.
44 * Relative paths can be used in pattern, which work from the pwd (though these are converted into
45 * full paths in the match).
47 int firstGlobChar = -1;
48 boolean foundGlobChar = false;
49 for (char c : new char[] { '*', '{', '?' })
51 if (pattern.indexOf(c) > -1
52 && (pattern.indexOf(c) < firstGlobChar || !foundGlobChar))
54 firstGlobChar = pattern.indexOf(c);
58 int lastFS = pattern.lastIndexOf(File.separatorChar, firstGlobChar);
61 String pS = pattern.substring(0, lastFS + 1);
62 String rest = pattern.substring(lastFS + 1);
63 Path parentDir = Paths.get(pS).toAbsolutePath();
64 if (parentDir.toFile().exists())
68 String glob = "glob:" + parentDir.toString() + File.separator
70 PathMatcher pm = FileSystems.getDefault().getPathMatcher(glob);
71 int maxDepth = rest.contains("**") ? 1028
73 .filter(ch -> ch == File.separatorChar).count())
76 Files.walkFileTree(parentDir,
77 EnumSet.of(FileVisitOption.FOLLOW_LINKS), maxDepth,
78 new SimpleFileVisitor<Path>()
81 public FileVisitResult visitFile(Path path,
82 BasicFileAttributes attrs) throws IOException
86 files.add(path.toFile());
88 return FileVisitResult.CONTINUE;
92 public FileVisitResult visitFileFailed(Path file,
93 IOException exc) throws IOException
95 return FileVisitResult.CONTINUE;
98 } catch (IOException e)
107 File f = new File(pattern);
108 if (allowSingleFilenameThatDoesNotExist || f.exists())
113 Collections.sort(files);
118 public static List<String> getFilenamesFromGlob(String pattern)
120 // convert list of Files to list of File.getPath() Strings
121 return getFilesFromGlob(pattern).stream().map(f -> f.getPath())
122 .collect(Collectors.toList());
125 public static String substituteHomeDir(String path)
127 return path.startsWith("~" + File.separator)
128 ? System.getProperty("user.home") + path.substring(1)
133 * This method returns the basename of File file
135 public static String getBasename(File file)
137 return getBasenameOrExtension(file, false);
141 * This method returns the extension of File file.
143 public static String getExtension(File file)
145 return getBasenameOrExtension(file, true);
148 public static String getBasenameOrExtension(File file, boolean extension)
154 String filename = file.getName();
155 int lastDot = filename.lastIndexOf('.');
156 if (lastDot > 0) // don't truncate if starts with '.'
158 value = extension ? filename.substring(lastDot + 1)
159 : filename.substring(0, lastDot);
163 value = extension ? "" : filename;
169 * This method returns the dirname of the first --append or --open value.
170 * Used primarily for substitutions in output filenames.
172 public static String getDirname(File file)
177 String dirname = null;
180 File p = file.getParentFile();
181 File d = new File(substituteHomeDir(p.getPath()));
182 dirname = d.getCanonicalPath();
183 } catch (IOException e)
186 "Exception when getting dirname of '" + file.getPath() + "'",