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(); } }