JAL-2937 extract reusable utility method
[jalview.git] / src / jalview / util / FileUtils.java
1 package jalview.util;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 /**
7  * Miscellaneous file-related functions
8  */
9 public final class FileUtils
10 {
11
12   /**
13    * Answers the executable file for the given command, or null if not found or
14    * not executable. The path to the executable is the command name prefixed by
15    * the given folder path, optionally with .exe appended.
16    * 
17    * @param cmd
18    *          command short name, for example hmmbuild
19    * @param binaryPath
20    *          parent folder for the executable
21    * @return
22    */
23   public static File getExecutable(String cmd, String binaryPath)
24   {
25     File file = new File(binaryPath, cmd);
26     if (!file.canExecute())
27     {
28       file = new File(binaryPath, cmd + ".exe");
29       {
30         if (!file.canExecute())
31         {
32           file = null;
33         }
34       }
35     }
36     return file;
37   }
38
39   /**
40    * A convenience method to create a temporary file that is deleted on exit of
41    * the JVM
42    * 
43    * @param prefix
44    * @param suffix
45    * @return
46    * @throws IOException
47    */
48   public static File createTempFile(String prefix, String suffix)
49           throws IOException
50   {
51     File f = File.createTempFile(prefix, suffix);
52     f.deleteOnExit();
53     return f;
54   }
55
56 }