void (int stage) { final String removedSuffix = stage == 0? ".remove" : ".remove" + stage; final String newSuffix = stage == 0? ".new" : ".new" + stage; FileUtil.processFilesRecursively(baseDir, file -> { if (file.getName().endsWith(removedSuffix)) { FileUtil.delete(getTargetFile(file, removedSuffix)); } return true; }); final long[] timestamp = {0}; FileUtil.processFilesRecursively(baseDir, file -> { try { if (file.getName().endsWith(newSuffix)) { File targetFile = getTargetFile(file, newSuffix); FileUtil.copyContent(file, targetFile); timestamp[0] = Math.max(timestamp[0], FSOperations.lastModified(targetFile)); } } catch (IOException e) { throw new RuntimeException(e); } return true; }); sleepUntil(timestamp[0]); }