4 import java.io.IOException;
5 import java.nio.file.FileSystems;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.nio.file.PathMatcher;
9 import java.util.ArrayList;
10 import java.util.List;
13 * Miscellaneous file-related functions
15 public final class FileUtils
19 * Answers the executable file for the given command, or null if not found or
20 * not executable. The path to the executable is the command name prefixed by
21 * the given folder path, optionally with .exe appended.
24 * command short name, for example hmmbuild
26 * parent folder for the executable
29 public static File getExecutable(String cmd, String binaryPath)
31 File file = new File(binaryPath, cmd);
32 if (!file.canExecute())
34 file = new File(binaryPath, cmd + ".exe");
36 if (!file.canExecute())
46 * Answers the path to the folder containing the given executable file, by
47 * searching the PATH environment variable. Answers null if no such executable
53 public static String getPathTo(String cmd)
55 String paths = System.getenv("PATH");
56 // backslash is to escape regular expression argument
57 for (String path : paths.split("\\" + File.pathSeparator))
59 if (getExecutable(cmd, path) != null)
68 * A convenience method to create a temporary file that is deleted on exit of
76 public static File createTempFile(String prefix, String suffix)
79 File f = File.createTempFile(prefix, suffix);
85 * Answers a (possibly empty) list of file paths found by searching below
86 * <code>from</code> that match the supplied regular expression
87 * <code>pattern</code>. Results may include <code>from</code> itself if it
88 * matches. If an exception occurs it is written to syserr and any results up
89 * to that point are returned. Note that the regular expression match applies
90 * to the whole path of any matched file.
92 * Because the whole directory tree below <code>from</code> is searched, this
93 * method may be slow if used for a high level directory.
97 * findMatchingPaths(".*/chimera.exe$", Paths.get("C:/Program Files"))
103 * @see https://stackoverflow.com/questions/794381/how-to-find-files-that-match-a-wildcard-string-in-java/31685610#comment62441832_31685610
105 public static List<String> findMatchingPaths(String pattern, Path from)
107 List<String> matches = new ArrayList<>();
108 PathMatcher pathMatcher = FileSystems.getDefault()
109 .getPathMatcher("regex:" + pattern);
112 Files.walk(from).filter(pathMatcher::matches)
113 .forEach(m -> matches.add(m.toString()));
114 } catch (IOException e)
117 "Error searching for " + pattern + " : " + e.toString());