-
Notifications
You must be signed in to change notification settings - Fork 6
how to Use files
Nuno Aguiar edited this page Jun 27, 2019
·
1 revision
All the functions to read and write files are organized under the object "IO" that can easily be use throw the variable "io".
var a_string = io.readFileString("abc123.txt");
All the contents of the "abc123.txt" file will assigned to the variable "a_string".
If the file is in a zip file you can use the shortcut ('::'):
var a_string = io.readFileString("aZipFile.zip::abc123.txt")
var an_array = io.readFileAsArray("abc123.txt");
print(an_array[0]); // Print the first line
print(an_array[1]); // Print the second line
The variable "an_array" will have an entry for each line on the file "abc123.txt".
var a_json = io.readFile("abc123.json");
print(a_json.something); // prints the element 'something' from the json object
If the file is in a zip file you can use the shortcut ('::'):
var a_json = io.readFile("aZipFile.zip::abc123.txt");
var a_json = io.readFileYAML("abc123.yaml");
print(a_json.something); // prints the element 'something' from the json object
Reads a json object from a YAML file.
var a_string = "The first line\nThe second line";
io.writeFileString("abc123.txt", a_string);
If you want to append lines instead of rewriting the entire file:
io.writeFileString("abc123.log", "Another line\n", void 0, true);
var lines = [];
lines.push("The first line");
lines.push("The second line");
io.writeFileAsArray("abc123.txt", lines);
var obj = { x: -1, y: 1 };
io.writeFile("abc123.json", obj);
var obj = { x: -1, y: 1 };
io.writeFileYAML("abc123.yaml", obj);
io.readLinesNDJSON("abc123.json", (line) => { sprint(line); });
var logline = { date: now(), level: "INFO", message: "a log message" };
io.writeLineNDJSON("abc123.json", logline);