1 package org.simantics.scl.runtime.procedure;
3 import java.io.BufferedWriter;
4 import java.io.IOException;
5 import java.nio.charset.Charset;
6 import java.nio.charset.MalformedInputException;
7 import java.nio.file.Files;
8 import java.nio.file.Paths;
9 import java.nio.file.StandardOpenOption;
10 import java.util.List;
12 public class StringIO {
13 public static final Charset UTF8 = Charset.forName("UTF-8");
15 public static List<String> readLines(String fileName) throws IOException {
17 return Files.readAllLines(Paths.get(fileName), UTF8);
18 } catch(MalformedInputException e) {
19 throw new RuntimeException("Encoding of the file '" + fileName + "' does not conform to UTF-8 (without BOM).");
23 public static List<String> readLinesWithCharset(String charset, String fileName) throws IOException {
25 return Files.readAllLines(Paths.get(fileName), Charset.forName(charset));
26 } catch(MalformedInputException e) {
27 throw new RuntimeException("Encoding of the file '" + fileName + "' does not conform to " + charset + ".");
31 public static String readContentsWithCharset(String charset, String fileName) throws IOException {
33 Charset cs = Charset.forName(charset);
34 return new String(Files.readAllBytes(Paths.get(fileName)), cs);
35 } catch(MalformedInputException e) {
36 throw new RuntimeException("Encoding of the file '" + fileName + "' does not conform to " + charset + ".");
40 public static void writeLines(String fileName, List<String> lines) throws IOException {
41 BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8);
42 for(String line : lines) {
49 public static void appendLine(String fileName, String line) throws IOException {
50 BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8,
51 StandardOpenOption.CREATE, StandardOpenOption.APPEND);