public static void toFile()

in log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/util/XmlWriter.java [45:62]


    public static void toFile(
            final Path filepath,
            final String rootElementName,
            final BiConsumer<Document, Element> documentConsumer) {
        try {
            final String xml = toString(rootElementName, documentConsumer);
            final byte[] xmlBytes = xml.getBytes(CharsetUtils.CHARSET);
            @Nullable
            final Path filepathParent = filepath.getParent();
            if (filepathParent != null) {
                Files.createDirectories(filepathParent);
            }
            Files.write(filepath, xmlBytes, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
        } catch (final Exception error) {
            final String message = String.format("failed writing XML to file `%s`", filepath);
            throw new RuntimeException(message, error);
        }
    }