System File object provide set of functions for working with files and folders


Lib.Sys.IO.File.getContent(path)

Retrieves the content of the file specified by path as a string.
If the file does not exist or can not be read, an exception is thrown.
Lib.Sys.FileSystem.exists can be used to check for existence.
If path is nil, the result is unspecified.

path - file path location

Lib.Sys.IO.File.saveContent(path, content)

Stores content in the file specified by path.
If the file cannot be written to, an exception is thrown.
If path or content are nil, the result is unspecified.

path - file path location

content - file text content

Lib.Sys.IO.File.getBytes(path)

Retrieves the binary content of the file specified by path.
If the file does not exist or can not be read, an exception is thrown.
Lib.Sys.FileSystem.exists can be used to check for existence.
If path is nil, the result is unspecified.

path - file path location

Lib.Sys.IO.File.saveBytes(path, bytes)

Stores bytes in the file specified by path in binary mode.
If the file cannot be written to, an exception is thrown.
If path or bytes are nil, the result is unspecified.

path - file path location

bytes - Bytes object

Lib.Sys.IO.File.read(path, binary)

Returns an FileInput handle to the file specified by path.
If binary is true, the file is opened in binary mode. Otherwise it is opened in non-binary mode.
If the file does not exist or can not be read, an exception is thrown.
Operations on the returned FileInput handle read on the opened file.
File handles should be closed via FileInput.close once the operation is complete.
If path is nil, the result is unspecified.

path - file path location

binary - file type binary or not

Lib.Sys.IO.File.write(path, binary)

Returns an FileOutput handle to the file specified by path.
If binary is true, the file is opened in binary mode. Otherwise it is opened in non-binary mode.
If the file cannot be written to, an exception is thrown.
Operations on the returned FileOutput handle write to the opened file.
If the file existed, its previous content is overwritten.
File handles should be closed via FileOutput.close once the operation is complete.
If path is nil, the result is unspecified.

path - file path location

binary - file type binary or not

Lib.Sys.IO.File.append(path, binary)

Similar to Lib.Sys.IO.File.write, but appends to the file if it exists instead of overwriting its contents.

path - file path location

binary - file type binary or not

Lib.Sys.IO.File.copy(srcPath, dstPath)

Copies the contents of the file specified by srcPath to the file specified by dstPath.
If the srcPath does not exist or cannot be read, or if the dstPath file cannot be written to, an exception is thrown.
If the file at dstPath exists, its contents are overwritten.
If srcPath or dstPath are nil, the result is unspecified.

srcPath - source file path location

dstPath - destination file path location


Examples:


root = Lib.Media.FileSystem.File.applicationStorageDirectory.nativePath

if not Lib.Sys.FileSystem.exists(root) then

       Lib.Sys.FileSystem.createDirectory(root)

end

path = root.."/README.txt"


Lib.Sys.IO.File.saveContent(path, "A SiMpLe FiLe")

if Lib.Media.System.systemName() == "ios" then

       Lib.Media.FileSystem.File.new(path).preventBackup = true

end


ba = Lib.Media.Utils.ByteArray.fromBytes(Lib.Sys.IO.File.getBytes(path))

print("length: " .. ba.length)

dkdkd = ba.asString()

print("file contents: " .. dkdkd)


ba.writeUTFBytes("MODIFIED FILE ///////////////////////")


print("now writing as another file and re-reading")

Lib.Sys.IO.File.saveBytes(path, ba)


ba2 = Lib.Media.Utils.ByteArray.fromBytes(Lib.Sys.IO.File.getBytes(path))

print("length: " .. ba2.length)

dkdkd2 = ba2.asString()

print("file contents: " .. dkdkd2)

Created with the Personal Edition of HelpNDoc: Easy EPub and documentation editor