Skip to content

FilesystemFile

STBrian edited this page Oct 28, 2025 · 1 revision

FilesystemFile is a type returned by Core.Filesystem.open function when it succeeds. It handles all functions to read/write from files

Methods

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

FilesystemFile:read

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?

Example

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")
end

FilesystemFile:write

It 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?) -> boolean

Example

local 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")
end

FilesystemFile:tell

It returns the current position in the file

FilesystemFile:tell() -> integer

Example

local 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")
end

FilesystemFile:flush

Flushes all file data in the write buffer. Not needed but sometimes may be useful

FilesystemFile:flush() -> boolean

FilesystemFile:seek

Sets 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) -> integer

Example

local 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")
end

FilesystemFile:isOpen

Checks if the file is open, useful if you are not sure if the file was previously closed

FilesystemFile:isOpen() -> boolean

FilesystemFile:isEOF

Checks if the current position is at the end of the file

FilesystemFile:isEOF() -> boolean

FilesystemFile:close

Closes 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()

Clone this wiki locally