From e596119a728be94c77e43d54be2b9402c596db2f Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Fri, 5 Mar 2021 12:51:19 +0200 Subject: [PATCH] Always close written file explicitly in StringIO.{writeLines,appendLine} gitlab #675 (cherry picked from commit 3969cd4b3fbbe35583656c3f48d6ba4902baa9e5) --- .../scl/runtime/procedure/StringIO.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/procedure/StringIO.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/procedure/StringIO.java index 82d3a9f32..1e1f5ef69 100644 --- a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/procedure/StringIO.java +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/procedure/StringIO.java @@ -38,20 +38,20 @@ public class StringIO { } public static void writeLines(String fileName, List lines) throws IOException { - BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8); - for(String line : lines) { - writer.write(line); - writer.write("\n"); + try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8)) { + for(String line : lines) { + writer.write(line); + writer.write("\n"); + } } - writer.close(); } public static void appendLine(String fileName, String line) throws IOException { - BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8, - StandardOpenOption.CREATE, StandardOpenOption.APPEND); - writer.write(line); - writer.write("\n"); - writer.close(); + try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8, + StandardOpenOption.CREATE, StandardOpenOption.APPEND)) { + writer.write(line); + writer.write("\n"); + } } } -- 2.47.1