Skip to main content
Version: latest - a1.172.0 โš–๏ธ

๐Ÿ“ File

A File represents an entry to a system file


info

It is not possible to open files from outside the server folder. All path must be relative to the Server's executable folder. All files are opened as binary file by default.

๐ŸŽ’ Examplesโ€‹

Server/Index.lua
local configuration_file = File("my_awesome_configuration.json")

local configuration_file_json = JSON.parse(configuration_file:Read())

๐Ÿ“‚ File Access & Sandboxingโ€‹

Access to files is sandboxed. Only certain directories and files are allowed to be written or read from.

Allowed Extensionsโ€‹

Only the following file extensions are allowed to be accessed (read & write):

.txt .dat .json .toml .xml .csv .log .png .jpg .jpeg .webp .webm .mp3 .wav .ogg .mp4

note

If you think that a file extension should be added to the allowed list, please report it in the Feedback Hub.

Server Sideโ€‹

On Server side, it's only allowed to access files inside the server folder itself (where server executable is located). Also, accessing the Config.toml is not allowed.

The default directory is where the server executable is located.

Example accessing a file from a Package: Packages/My-Package/Server/MyFile.json.

Client Sideโ€‹

On the client side, we have the concept of the .transient/ folder which is a folder automatically generated inside the client's cached Packages/ folder, where files can be created in a more persistent way.

It's not allowed to access files outside the client's cache folder Packages/, and write access is only permitted inside the .transient/ folder itself.

The default directory is in the client's cached Packages/.transient/ folder.

Example accessing a file from a Package (with read-only access): ../my-package/Client/MyFile.json.

Example accessing a transient file (with write access): MyFile.json.

๐Ÿ›  Constructorsโ€‹

Default Constructorโ€‹

No description provided

local my_file = File(file_path, truncate?)
Parameters
NameTypeDescription
file_pathstringIf on Server, this is the Path relative to server executable. Otherwise if on Client, this is the Path relative to Client's 'Packages/.transient/' folder
truncate?booleanWhether or not to clear the file upon opening it Default false

๐Ÿ—ฟ Static Functionsโ€‹

Directories
CreateDirectory: booleanCreates a Directory (for every folder passed)
GetDirectories: table of stringGets a list of all directories given a path
IsDirectory: booleanChecks if a path is a directory
Paths
Exists: booleanVerifies if an entry exists in the file system
GetFiles: table of stringGets a list of all files in a directory
GetFullPath: stringGets the full path given a relative path based on the current side (client or server)
IsRegularFile: booleanChecks if a path is a file
Remove: integerDeletes a folder or file
Rename: booleanRenames or moves a folder or file
Time: integerReturns when a file was last modified in Unix time

Directoriesstatic functionsโ€‹

CreateDirectorySlowโ€‹

Creates a Directory (for every folder passed)

local ret = File.CreateDirectory(path)
Parameters
NameTypeDescription
pathstringPath to folder
Returns
TypeDescription
booleanif succeeded

GetDirectoriesSlowโ€‹

Gets a list of all directories given a path

Filters are optional. Note that the results may differ between Linux and Windows due to the way the file system works

local ret = File.GetDirectories(path_filter?, max_depth?)
Parameters
NameTypeDescription
path_filter?stringPath filter Default
max_depth?integerThe maximum depth to go further in the folders while searching. Pass -1 for maximum depth Default -1
Returns
TypeDescription
table of stringList of directories

IsDirectoryModerateโ€‹

Checks if a path is a directory

local ret = File.IsDirectory(path)
Parameters
NameTypeDescription
pathstringPath to folder
Returns
TypeDescription
booleanWhether the path is a directory

Pathsstatic functionsโ€‹

ExistsModerateโ€‹

Verifies if an entry exists in the file system

local ret = File.Exists(path)
Parameters
NameTypeDescription
pathstringPath to file or folder
Returns
TypeDescription
booleanif exists

GetFilesSlowโ€‹

Gets a list of all files in a directory

Filters are optional. Note that the results may differ between Linux and Windows due to the way the file system works

local ret = File.GetFiles(path_filter?, extension_filter?, max_depth?)
Parameters
NameTypeDescription
path_filter?string or tablePath filter Default
extension_filter?stringE.g.: .lua Default
max_depth?integerThe maximum depth to go further in the folders while searching. Pass -1 for maximum depth Default -1
Returns
TypeDescription
table of stringList of files

GetFullPathModerateโ€‹

Gets the full path given a relative path based on the current side (client or server)

local ret = File.GetFullPath(path)
Parameters
NameTypeDescription
pathstringPath to file or directory
Returns
TypeDescription
stringthe full path parsed

IsRegularFileModerateโ€‹

Checks if a path is a file

local ret = File.IsRegularFile(path)
Parameters
NameTypeDescription
pathstringPath to file
Returns
TypeDescription
booleanWhether the path is a regular file

RemoveSlowโ€‹

Deletes a folder or file

local ret = File.Remove(path)
Parameters
NameTypeDescription
pathstringPath to file or folder
Returns
TypeDescription
integeramount of files deleted

RenameSlowโ€‹

Renames or moves a folder or file

local ret = File.Rename(old_path, new_path)
Parameters
NameTypeDescription
old_pathstringPath to the existing file or folder
new_pathstringNew path of the file or folder
Returns
TypeDescription
booleanif it was successfully renamed

TimeSlowโ€‹

Returns when a file was last modified in Unix time

local ret = File.Time(path)
Parameters
NameTypeDescription
pathstringPath to file
Returns
TypeDescription
integerthe last update time in unix time

๐Ÿฆ  Functionsโ€‹

Contents
Read: stringReads characters from the File and returns it. Also moves the file pointer to the latest read position. Pass 0 to read the whole file
ReadAsyncReads characters from the File asynchronously
ReadJSON: tableReads the whole file as a JSON and returns it
ReadJSONAsyncReads the whole file as a JSON and returns it asynchronously
ReadLine: stringReads and returns the next file line
WriteWrites the Data at the current position of the file
Position
SeekSets the file pointer to a specific position
Size: integerReturns the size of the file
SkipSkips n (amount) positions from the current file pointer position
Tell: integerReturns the current file pointer position
Streams
CloseCloses the file and destroys the entity
FlushFlushes content to the file
HasFailed: booleanChecks if the last operation has Failed
IsBad: booleanChecks if the file status is Bad
IsEOF: booleanChecks if the file status is End of File
IsGood: booleanChecks if the file status is Good

Contentsfunctionsโ€‹

ReadBlockingโ€‹

Reads characters from the File and returns it. Also moves the file pointer to the latest read position. Pass 0 to read the whole file

local ret = my_file:Read(length?)
Parameters
NameTypeDescription
length?integerLength to be read from file, leave it empty (or 0) to read the whole file Default 0
Returns
TypeDescription
stringfile data

ReadAsyncOptimalโ€‹

Reads characters from the File asynchronously

my_file:ReadAsync(length?, callback)
Parameters
NameTypeDescription
length?integerLength to be read from file, pass 0 to read the whole file Default 0
callbackfunctionCallback with this format

ReadJSONBlockingโ€‹

Reads the whole file as a JSON and returns it

local ret = my_file:ReadJSON()
Returns
TypeDescription
tableparsed table

ReadJSONAsyncOptimalโ€‹

Reads the whole file as a JSON and returns it asynchronously

my_file:ReadJSONAsync(callback)
Parameters
NameTypeDescription
callbackfunctionCallback with the file read with this format

ReadLineBlockingโ€‹

Reads and returns the next file line

local ret = my_file:ReadLine()
Returns
TypeDescription
stringfile line data

WriteSlowโ€‹

Writes the Data at the current position of the file

my_file:Write(data)
Parameters
NameTypeDescription
datastringData to write to the file

Positionfunctionsโ€‹

SeekModerateโ€‹

Sets the file pointer to a specific position

my_file:Seek(position)
Parameters
NameTypeDescription
positionintegerPosition to offset the file pointer

SizeOptimalโ€‹

Returns the size of the file

local ret = my_file:Size()
Returns
TypeDescription
integerfile size

SkipOptimalโ€‹

Skips n (amount) positions from the current file pointer position

my_file:Skip(amount)
Parameters
NameTypeDescription
amountintegerAmount to offset the file pointer

TellOptimalโ€‹

Returns the current file pointer position

local ret = my_file:Tell()
Returns
TypeDescription
integercurrent file pointer position

Streamsfunctionsโ€‹

CloseBlockingโ€‹

Closes the file and destroys the entity

my_file:Close()

FlushBlockingโ€‹

Flushes content to the file

my_file:Flush()

HasFailedOptimalโ€‹

Checks if the last operation has Failed

local ret = my_file:HasFailed()
Returns
TypeDescription
booleanif last operation failed

IsBadOptimalโ€‹

Checks if the file status is Bad

local ret = my_file:IsBad()
Returns
TypeDescription
booleanif status is Bad

IsEOFOptimalโ€‹

Checks if the file status is End of File

local ret = my_file:IsEOF()
Returns
TypeDescription
booleanif is EOF

IsGoodOptimalโ€‹

Checks if the file status is Good

local ret = my_file:IsGood()
Returns
TypeDescription
booleanif status is Good

๐Ÿš€ Eventsโ€‹

This class doesn't have own events.