@author pavlo
| 27 | * @author pavlo |
| 28 | */ |
| 29 | public abstract class FileUtil { |
| 30 | |
| 31 | private static final Pattern EXT_SPLIT = Pattern.compile("\\."); |
| 32 | |
| 33 | /** |
| 34 | * Join path components |
| 35 | * |
| 36 | * @param args |
| 37 | * @return |
| 38 | */ |
| 39 | public static String joinPath(String... args) { |
| 40 | StringBuilder result = new StringBuilder(); |
| 41 | boolean first = true; |
| 42 | for (String a : args) { |
| 43 | if (a != null && a.length() > 0) { |
| 44 | if (!first) { |
| 45 | result.append("/"); |
| 46 | } |
| 47 | result.append(a); |
| 48 | first = false; |
| 49 | } |
| 50 | } |
| 51 | return result.toString(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Given a basename for a file, find the next possible filename if this file already exists. For |
| 56 | * example, if the file test.res already exists, create a file called, test.1.res |
| 57 | * |
| 58 | * @param basename |
| 59 | * @return |
| 60 | */ |
| 61 | public static String getNextFilename(String basename) { |
| 62 | |
| 63 | if (!exists(basename)) return basename; |
| 64 | |
| 65 | File f = new File(basename); |
| 66 | if (f != null && f.isFile()) { |
| 67 | String parts[] = EXT_SPLIT.split(basename); |
| 68 | |
| 69 | // Check how many files already exist |
| 70 | int counter = 1; |
| 71 | String nextName = parts[0] + "." + counter + "." + parts[1]; |
| 72 | while (exists(nextName)) { |
| 73 | ++counter; |
| 74 | nextName = parts[0] + "." + counter + "." + parts[1]; |
| 75 | } |
| 76 | return nextName; |
| 77 | } |
| 78 | |
| 79 | // Should we throw instead?? |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | public static boolean exists(String path) { |
| 84 | return (new File(path).exists()); |
| 85 | } |
| 86 |
nothing calls this directly
no outgoing calls
no test coverage detected