]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Always close written file explicitly in StringIO.{writeLines,appendLine}
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 5 Mar 2021 10:51:19 +0000 (12:51 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 4 May 2021 08:35:12 +0000 (11:35 +0300)
gitlab #675

(cherry picked from commit 3969cd4b3fbbe35583656c3f48d6ba4902baa9e5)

bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/procedure/StringIO.java

index 82d3a9f326f772843c086a2bc43d281f9e45c3f2..1e1f5ef69544588e7d9fe422d4b1c7608a0d6adc 100644 (file)
@@ -38,20 +38,20 @@ public class StringIO {
     }
 
     public static void writeLines(String fileName, List<String> 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");
+        }
     }
 
 }