]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/procedure/StringIO.java
Added StringIO.readContentsWithCharset
[simantics/platform.git] / bundles / org.simantics.scl.runtime / src / org / simantics / scl / runtime / procedure / StringIO.java
index 8cf8ccec123fed8a042592350721580d3358542c..82d3a9f326f772843c086a2bc43d281f9e45c3f2 100644 (file)
@@ -1,47 +1,57 @@
-package org.simantics.scl.runtime.procedure;\r
-\r
-import java.io.BufferedWriter;\r
-import java.io.IOException;\r
-import java.nio.charset.Charset;\r
-import java.nio.charset.MalformedInputException;\r
-import java.nio.file.Files;\r
-import java.nio.file.Paths;\r
-import java.nio.file.StandardOpenOption;\r
-import java.util.List;\r
-\r
-public class StringIO {\r
-    public static final Charset UTF8 = Charset.forName("UTF-8"); \r
-    \r
-    public static List<String> readLines(String fileName) throws IOException {\r
-        try {\r
-            return Files.readAllLines(Paths.get(fileName), UTF8);\r
-        } catch(MalformedInputException e) {\r
-            throw new RuntimeException("Encoding of the file '" + fileName + "' does not conform to UTF-8 (without BOM).");\r
-        }\r
-    }\r
-    \r
-    public static List<String> readLinesWithCharset(String charset, String fileName) throws IOException {\r
-        try {\r
-            return Files.readAllLines(Paths.get(fileName), Charset.forName(charset));\r
-        } catch(MalformedInputException e) {\r
-            throw new RuntimeException("Encoding of the file '" + fileName + "' does not conform to " + charset + ".");\r
-        }\r
-    }\r
-    \r
-    public static void writeLines(String fileName, List<String> lines) throws IOException {\r
-        BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8);\r
-        for(String line : lines) {\r
-            writer.write(line);\r
-            writer.write("\n");\r
-        }\r
-        writer.close();\r
-    }\r
-    \r
-    public static void appendLine(String fileName, String line) throws IOException {\r
-        BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8,\r
-                StandardOpenOption.CREATE, StandardOpenOption.APPEND);\r
-        writer.write(line);\r
-        writer.write("\n");\r
-        writer.close();\r
-    }\r
-}\r
+package org.simantics.scl.runtime.procedure;
+
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.MalformedInputException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.util.List;
+
+public class StringIO {
+    public static final Charset UTF8 = Charset.forName("UTF-8"); 
+    
+    public static List<String> readLines(String fileName) throws IOException {
+        try {
+            return Files.readAllLines(Paths.get(fileName), UTF8);
+        } catch(MalformedInputException e) {
+            throw new RuntimeException("Encoding of the file '" + fileName + "' does not conform to UTF-8 (without BOM).");
+        }
+    }
+    
+    public static List<String> readLinesWithCharset(String charset, String fileName) throws IOException {
+        try {
+            return Files.readAllLines(Paths.get(fileName), Charset.forName(charset));
+        } catch(MalformedInputException e) {
+            throw new RuntimeException("Encoding of the file '" + fileName + "' does not conform to " + charset + ".");
+        }
+    }
+
+    public static String readContentsWithCharset(String charset, String fileName) throws IOException {
+        try {
+            Charset cs = Charset.forName(charset);
+            return new String(Files.readAllBytes(Paths.get(fileName)), cs);
+        } catch(MalformedInputException e) {
+            throw new RuntimeException("Encoding of the file '" + fileName + "' does not conform to " + charset + ".");
+        }
+    }
+
+    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");
+        }
+        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();
+    }
+
+}