X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.runtime%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fruntime%2Fprocedure%2FStringIO.java;h=82d3a9f326f772843c086a2bc43d281f9e45c3f2;hp=8cf8ccec123fed8a042592350721580d3358542c;hb=0807209928f01e95669af6aeb671110209774bc6;hpb=969bd23cab98a79ca9101af33334000879fb60c5 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 8cf8ccec1..82d3a9f32 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 @@ -1,47 +1,57 @@ -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 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 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 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"); - } - 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(); - } -} +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 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 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 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(); + } + +}