Skip to main content

IO

Read, write, and inspect files through Host.IO. All file paths use Host.Url objects.

Methods:

MethodReturnsParametersDescription
Host.IO.createPackage(path, type)objectpathHost.Url path, type — format (e.g. "application/zip")Create a .package archive. Returned object has setOption(), create(), embedd(folder), flush(), close()
Host.IO.createTextFile(path)objectpathHost.Url pathCreate a text file for writing (.writeLine(text), .close())
Host.IO.File(path)objectpathHost.Url pathGet a file operation object (.exists(), .copyTo(), .remove())
Host.IO.findFiles(folder, pattern)objectfolder — folder path, pattern — glob patternFind files matching a pattern.
Host.IO.fromBase64(data)stringdata — Base64 stringDecode Base64 data
Host.IO.getDevelopmentFileLocation()stringnoneGet the development file storage path
Host.IO.loadJsonFile(path)objectpathHost.Url pathLoad a JSON file into a native JavaScript object
Host.IO.toBase64(data)stringdata — string data to encodeEncode data as Base64
Host.IO.openPackage(path, type)objectpathHost.Url path, type — format (e.g. "application/zip")Open a .package archive. Returned object: extract(folder)
Host.IO.openTextFile(path)objectpathHost.Url pathOpen a text file for reading (.readLine(), .endOfStream, .close())
Host.IO.XmlTree(path)objectpathHost.Url pathParse an XML file into a navigable tree

Create Package

Host.IO.createPackage() returns a package builder object.

Methods:

MethodReturnsParametersDescription
setOption(name, value)name — option name, value — option valueSet a package option (e.g. "compressed", true)
create()noneCreate the package file
embedd(folder)folderHost.Url path to a folderEmbed all files from a folder into the package
flush()noneFlush pending writes
close()noneClose and finalize the package
var folder = Host.Url("local://$USERCONTENT/my-folder/");
var pkg = Host.IO.createPackage(path, "application/zip");
pkg.setOption("compressed", true);
pkg.create();
pkg.embedd(folder);
pkg.flush();
pkg.close();

Create Text File

Host.IO.createTextFile() returns a text file writer object.

var file = Host.IO.createTextFile(path);
if (file) {
file.writeLine("content");
file.close();
}

File Operations Object

Host.IO.File() returns a file operation object for the given path.

Methods:

MethodReturnsParametersDescription
exists()numbernoneCheck whether a file exists (returns 1 if found, 0 if not)
copyTo(dest)dest — destination Host.UrlCopy a file to a destination
remove()noneDelete a file or directory

Find Files

Host.IO.findFiles() returns an iterator of matching files as Host.Url objects.

Iterator methods:

MethodReturnsParametersDescription
done()numbernoneCheck whether iteration is complete (returns 0 while items remain, 1 when exhausted)
next()objectnoneGet the next file item
first()noneMove to the first item
last()noneMove to the last item
previous()objectnoneGet the previous file item
var it = Host.IO.findFiles(folder, "*.xml");
while (!it.done()) {
var file = it.next();
var name = file.name;
}

From Base64

Host.IO.fromBase64() decodes a Base64 string.

var decoded = Host.IO.fromBase64(data);

Load JSON File

Host.IO.loadJsonFile() loads and parses a JSON file.

var data = Host.IO.loadJsonFile(Host.Url("local://$USERCONTENT/myfile.json"));

Open Package

Host.IO.openPackage() opens an existing .package archive for reading.

Methods:

MethodReturnsParametersDescription
extract(folder)folderHost.Url path to extract toExtract all package contents to a folder
var pkg = Host.IO.openPackage(path, "application/zip");
if (pkg) {
var folder = Host.Url("memory://ExtractTarget/", true);
pkg.extract(folder);
}

Open Text File

Host.IO.openTextFile() opens a text file for reading.

var file = Host.IO.openTextFile(path);
if (file) {
while (!file.endOfStream) {
var line = file.readLine();
}
file.close();
}

To Base64

Host.IO.toBase64() encodes a string as Base64.

var encoded = Host.IO.toBase64(data);

XML Tree

Host.IO.XmlTree() parses an XML file into a navigable tree. The tree object has root and errorMessage properties. Nodes are returned by tree.root and node.findNode().

Tree properties:

PropertyTypeWritableExampleDescription
errorMessagestringNo""Error message, empty on success
rootobjectNoRoot node of the parsed XML

Tree methods:

MethodReturnsParametersDescription
loadFromFile()noneReload the XML from file
saveToFile()noneSave modifications back to file

Node properties:

PropertyTypeWritableExampleDescription
namestringNo"SomeSection"Tag name
textstringNo"value"Text content
commentstringNo"<!-- comment -->"Comment content
parentobjectNoParent node object

Node methods:

MethodReturnsParametersDescription
addChild(node)node — child node objectAdd a child node
find(name)objectname — tag nameFind first child with matching tag name (alias for findNode)
findNode(name)objectname — tag nameFind first child with matching tag name
getAttribute(name)stringname — attribute nameGet an attribute value
newIterator()objectnoneCreate an iterator over child nodes
newNode()objectnoneCreate a new child node
setAttribute(name, value)name — attribute name, value — attribute valueSet an attribute
var tree = Host.IO.XmlTree(Host.Url("local://$APPCONFIG/User.options"));
var root = tree.root;
var child = root.findNode("SomeSection");
var val = child.getAttribute("someAttr");

var it = root.newIterator();
while (!it.done()) {
var node = it.next();
Host.Console.writeLine(node.name);
}