-
Notifications
You must be signed in to change notification settings - Fork 2
FilesystemFile
FilesystemFile is a type returned by Core.Filesystem.open function when it succeeds. It handles all functions to read/write from files
| FilesystemFile:read | Reads from the file |
| FilesystemFile:write | Writes to a file |
| FilesystemFile:tell | Tells the current position in the file |
| FilesystemFile:flush | Flushes all data in the write buffer |
| FilesystemFile:seek | Changes the position in the file |
| FilesystemFile:isOpen | Checks is the file is open |
| FilesystemFile:isEOF | Checks if the file is at end of file |
| FilesystemFile:close | Manually closes the file |
It reads the specified amount of bytes, or you can use "*all" to read all file and returns the data in a string, or nil if an error occurs
FilesystemFile:read(amount: integer|"*all") -> string?local optionsFile = Core.Filesystem.open("extdata:/options.txt", "r")
if optionsFile then -- Checks that optionsFile is not nil, in other words, that the file opened successfully
local optionsData = optionsFile:read("*all")
--- < your code here >
else
Core.Debug.message("Failed to open extdata:/options.txt")
endIt writes the data provided, by default it will write all, but if specified it will write the provided amount of bytes
FilesystemFile:write(data: string, amount: integer?) -> booleanlocal myFile = Core.Filesystem.open("sdmc:/yourfile.txt", "w")
if myFile then -- Checks that the file is not nil
myFile:write("data to write")
--- < your code here >
else
Core.Debug.message("Failed to open file")
endIt returns the current position in the file
FilesystemFile:tell() -> integerlocal myFile = Core.Filesystem.open("sdmc:/yourfile.txt", "w")
if myFile then -- Checks that the file is not nil
myFile:write("data to write")
local pos = myFile:tell()
--- < your code here >
else
Core.Debug.message("Failed to open file")
endFlushes all file data in the write buffer. Not needed but sometimes may be useful
FilesystemFile:flush() -> booleanSets the position in file and returns the new position. By default it takes "set" as the origin if not provided origin, but you can use "cur" or "end" as well
FilesystemFile:seek(offset: integer, whence: "set"|"cur"|"end"|nil) -> integerlocal myFile = Core.Filesystem.open("sdmc:/yourfile.txt", "w+")
if myFile then -- Checks that the file is not nil
myFile:seek(0, "end")
myFile:write("data to write")
--- < your code here >
else
Core.Debug.message("Failed to open file")
endChecks if the file is open, useful if you are not sure if the file was previously closed
FilesystemFile:isOpen() -> booleanChecks if the current position is at the end of the file
FilesystemFile:isEOF() -> booleanCloses the file if it is open, otherwise does nothing. Is not strictly neccessary to use this method as the file will close when the lua gc collects this object if is still open, but it is still a good practice to explicity close the file
FilesystemFile:close()