Skip to main content
Version: bleeding-edge 🩸

📁 File

A File represents an entry to a system file.

💂Authority
This class can only be spawned on 🟦 Server.
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

local configuration_file = File("my_awesome_configuration.json")

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

🛠 Constructors

Default Constructor

local my_file = File(file_path, truncate?)
TypeNameDefaultDescription
string file_pathPath relative to server executable
boolean truncatefalseWhether or not to clear the file upon opening it

🗿 Static Functions

ReturnsNameDescription
integer TimeReturns when a file was last modified in Unix time
boolean CreateDirectoryCreates a Directory (for every folder passed)
integer RemoveDeletes a folder or file
boolean ExistsVerifies if a entry exists in the file system
boolean IsDirectoryChecks if a path is a directory
boolean IsRegularFileChecks if a path is a file

Time

Returns when a file was last modified in Unix time

Returns integer (the last update time in unix time)
local ret = File.Time(path)
TypeParameterDefaultDescription
string pathPath to file

CreateDirectory

Creates a Directory (for every folder passed)

Returns boolean (if succeeded)
local ret = File.CreateDirectory(path)
TypeParameterDefaultDescription
string pathPath to folder

Remove

Deletes a folder or file

Returns integer (amount of files deleted)
local ret = File.Remove(path)
TypeParameterDefaultDescription
string pathPath to file or folder

Exists

Verifies if a entry exists in the file system

Returns boolean (if exists)
local ret = File.Exists(path)
TypeParameterDefaultDescription
string pathPath to file or folder

IsDirectory

Checks if a path is a directory

Returns boolean (if is a directory)
local ret = File.IsDirectory(path)
TypeParameterDefaultDescription
string pathPath to folder

IsRegularFile

Checks if a path is a file

Returns boolean (if is a regular file)
local ret = File.IsRegularFile(path)
TypeParameterDefaultDescription
string pathPath to filey

🦠 Functions

ReturnsNameDescription
CloseCloses the file and destroys the entity
FlushFlushes content to the file
boolean IsEOFChecks if the file status is End of File
boolean IsBadChecks if the file status is Bad
boolean IsGoodChecks if the file status is Good
boolean HasFailedChecks if the last operation has Failed
string ReadReads 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.
string ReadLineReads and returns the next file line
table ReadJSONReads the whole file as a JSON and returns it.
ReadJSONAsyncReads the whole file as a JSON and returns it asynchronously.
SeekSets the file pointer to a specific position
integer SizeReturns the size of the file
SkipSkips n (amount) positions from the current file pointer position
integer TellReturns the current file pointer position
WriteWrites the Data at the current position of the file

Close

Closes the file and destroys the entity
my_file:Close()

Flush

Flushes content to the file
my_file:Flush()

IsEOF

Checks if the file status is End of File

Returns boolean (if is EOF)
local ret = my_file:IsEOF()

IsBad

Checks if the file status is Bad

Returns boolean (if status is Bad)
local ret = my_file:IsBad()

IsGood

Checks if the file status is Good

Returns boolean (if status is Good)
local ret = my_file:IsGood()

HasFailed

Checks if the last operation has Failed

Returns boolean (if last operation failed)
local ret = my_file:HasFailed()

Read

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

Returns string (file data)
local ret = my_file:Read(length?)
TypeParameterDefaultDescription
integer length?0Length to be read from file

ReadAsync

Reads characters from the File asynchronously.
my_file:ReadAsync(length, callback)
TypeParameterDefaultDescription
integer lengthLength to be read from file
function callbackCallback with the file read

ReadLine

Reads and returns the next file line

Returns string (file line data)
local ret = my_file:ReadLine()

ReadJSON

Reads the whole file as a JSON and returns it.

Returns table (parsed table)
local ret = my_file:ReadJSON()

ReadJSONAsync

Reads the whole file as a JSON and returns it asynchronously.
my_file:ReadJSONAsync(callback)
TypeParameterDefaultDescription
function callbackCallback with the file read

Seek

Sets the file pointer to a specific position
my_file:Seek(position)
TypeParameterDefaultDescription
integer positionPosition to offset the file pointer

Size

Returns the size of the file

Returns integer (file size)
local ret = my_file:Size()

Skip

Skips n (amount) positions from the current file pointer position
my_file:Skip(amount)
TypeParameterDefaultDescription
integer amountAmount to offset the file pointer

Tell

Returns the current file pointer position

Returns integer (current file pointer position)
local ret = my_file:Tell()

Write

Writes the Data at the current position of the file
my_file:Write(data)
TypeParameterDefaultDescription
string dataData to write to the file

🚀 Events

This entity doesn't have own events.