void (boolean removeDeleted) { final Set<String> allNames = new HashSet<>(); final Map<String, Path> templatesOnDisk = new HashMap<>(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(templateDir, file -> !Files.isDirectory(file) && !Files.isHidden(file))) { for (Path file : stream) { String fileName = file.getFileName().toString(); templatesOnDisk.put(fileName, file); allNames.add(fileName); } } catch (NoSuchFileException ignored) { } catch (IOException e) { LOG.error(e); } final Map<String, FileTemplateBase> templatesToSave = new HashMap<>(); for (FileTemplateBase template : getAllTemplates(true)) { processTemplate(allNames, templatesToSave, template); for (FileTemplate child : template.getChildren()) { processTemplate(allNames, templatesToSave, (FileTemplateBase)child); } } if (allNames.isEmpty()) { return; } try { Files.createDirectories(templateDir); } catch (IOException e) { LOG.info("Cannot create directory: " + templateDir); } final String lineSeparator = CodeStyle.getDefaultSettings().getLineSeparator(); for (String name : allNames) { final Path customizedTemplateFile = templatesOnDisk.get(name); final FileTemplateBase templateToSave = templatesToSave.get(name); if (customizedTemplateFile == null) { // template was not saved before try { saveTemplate(templateDir, templateToSave, lineSeparator); } catch (IOException e) { LOG.error("Unable to save template " + name, e); } } else if (templateToSave == null) { // template was removed if (removeDeleted) { try { Files.delete(customizedTemplateFile); } catch (IOException e) { LOG.error(e); } } } else { // both customized content on disk and corresponding template are present try { String diskText = StringUtilRt.convertLineSeparators(Files.readString(customizedTemplateFile)); String templateText = templateToSave.getText(); if (!diskText.equals(templateText)) { // save only if texts differ to avoid unnecessary file touching saveTemplate(templateDir, templateToSave, lineSeparator); } } catch (IOException e) { LOG.error("Unable to save template " + name, e); } } } }