Copies from src to dst and performs a simple copy-replace templating operation along the way. ```java copyFile(src, dst, "%username%", "lskywalker" "%firstname%", "Luke", "%lastname%", "Skywalker"); ```
(File srcFile, File dstFile, String... toReplace)
| 239 | * ``` |
| 240 | */ |
| 241 | public static void copyFile(File srcFile, File dstFile, String... toReplace) throws IOException { |
| 242 | // make a map of the keys that we're replacing |
| 243 | Preconditions.checkArgument(toReplace.length % 2 == 0); |
| 244 | Map<String, String> replaceMap = Maps.newHashMap(); |
| 245 | for (int i = 0; i < toReplace.length / 2; ++i) { |
| 246 | replaceMap.put(toReplace[2 * i], toReplace[2 * i + 1]); |
| 247 | } |
| 248 | // replace them |
| 249 | String content = Joiner.on("\n").join(Files.readLines(srcFile, StandardCharsets.UTF_8)); |
| 250 | for (Entry<String, String> entry : replaceMap.entrySet()) { |
| 251 | content = content.replace(entry.getKey(), entry.getValue()); |
| 252 | } |
| 253 | // write it out |
| 254 | mkdirs(dstFile.getParentFile()); |
| 255 | Files.write(content.getBytes(StandardCharsets.UTF_8), dstFile); |
| 256 | } |
| 257 | |
| 258 | /** Modifies the given file in place. */ |
| 259 | public static void modifyFile(File file, Function<String, String> modifier) throws IOException { |
no test coverage detected