# StringIO
-# Undocumented entities
+This module contains file I/O related functions that read and
+write contents of textual files into/from String instances.
-::undocumented[]
\ No newline at end of file
+## Reading
+
+::value[readLines]
+::value[readLinesWithCharset]
+::value[readContentsWithCharset]
+
+## Writing
+
+::value[writeLines]
+::value[appendLine]
importJava "org.simantics.scl.runtime.procedure.StringIO" where
- "Reads all lines of the file whose name is given as a parameter"
+ """
+ Reads all lines of the file whose name is given as a parameter.
+ The file contents are expected to be UTF8 encoded.
+ """
readLines :: String -> <Proc> [String]
"`readLinesWithCharset \"charset\" \"file\"` reads all lines of the file whose name is given as a parameter using the given charset."
readLinesWithCharset :: String -> String -> <Proc> [String]
- "Creates a new file with the given file name whose contents are the given lines."
+ "`readContentsWithCharset \"charset\" \"file\"` reads all lines of the file whose name is given as a parameter using the given charset."
+ readContentsWithCharset :: String -> String -> <Proc> String
+
+ """
+ Creates a new file with the given file name whose contents are the given lines.
+ The written file will be UTF8 encoded.
+ """
writeLines :: String -> [String] -> <Proc> ()
- "`appendLine fileName line` appends `line` to the file with name `fileName`"
+ """
+ `appendLine fileName line` appends `line` to the file with name `fileName`
+ The appended content will be UTF8 encoded.
+ """
appendLine :: String -> String -> <Proc> ()
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("\n");
writer.close();
}
+
}