From 5409baec50869b7559ddefaa07c152afa8c31103 Mon Sep 17 00:00:00 2001 From: janos erdos Date: Fri, 16 Aug 2024 13:07:07 +0200 Subject: [PATCH] add API to EvaluatedDocument --- .../github/erdos/stencil/EvaluatedDocument.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/java-src/io/github/erdos/stencil/EvaluatedDocument.java b/java-src/io/github/erdos/stencil/EvaluatedDocument.java index 1232f855..fc8379f4 100644 --- a/java-src/io/github/erdos/stencil/EvaluatedDocument.java +++ b/java-src/io/github/erdos/stencil/EvaluatedDocument.java @@ -3,6 +3,8 @@ import io.github.erdos.stencil.impl.InputStreamExceptionPropagation; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.concurrent.ExecutorService; /** @@ -17,10 +19,17 @@ public interface EvaluatedDocument { * Writes output of this document to a file */ default void writeToFile(File output) throws IOException { - if (output.exists()) { - throw new IllegalArgumentException("Output file already exists: " + output); + writeToPath(output.toPath()); + } + + /** + * Writes output of this document to a path + */ + default void writeToPath(Path output) throws IOException { + if (Files.exists(output)) { + throw new IllegalArgumentException("Output path already exists: " + output); } - try (FileOutputStream fos = new FileOutputStream(output)) { + try (OutputStream fos = Files.newOutputStream(output)) { write(fos); } }