Skip to main content

File

Description:

  Represents a file on the filesystem. A function that takes a file path can also take a File.

type

Type: Function.

Description:

  Gets the type of the object as a string.

Signature:

type: function(self: File): string

Returns:

Return TypeDescription
stringThe type as a string.

Of

Type: Function.

Description:

  Checks whether an object is of a certain type. If the object has the type with the specified name in its hierarchy, this function will return true.

Signature:

typeOf: function(self: File, type_name: string): boolean

Parameters:

ParameterTypeDescription
type_namestringThe name of the type to check for.

Returns:

Return TypeDescription
booleanTrue if the object is of the specified type, false otherwise.

release

Type: Function.

Description:

  Destroys the object's Lua reference. The object will be completely deleted if it's not referenced by any other LÖVE object or thread. This method can be used to immediately clean up resources without waiting for Lua's garbage collector.

Signature:

release: function(self: File): boolean

Returns:

Return TypeDescription
booleanTrue if the object was released by this call, false if it had been previously released.

open

Type: Function.

Description:

  Open the file for write, read or append. Overload details:

  1. If you are getting the error message 'Could not set write directory', try setting the save directory. This is done either with love.filesystem.setIdentity or by setting the identity field in love.conf (only available with love 0.7 or higher).

Signature:

open: function(self: File, mode: FileMode): boolean | nil, string | nil

Parameters:

ParameterTypeDescription
modeFileModeThe mode to open the file in.

Returns:

Return TypeDescription
boolean or nilTrue on success, false otherwise.
string or nilThe error string if an error occurred.

close

Type: Function.

Description:

  Closes a File.

Signature:

close: function(self: File): boolean

Returns:

Return TypeDescription
booleanWhether closing was successful.

isOpen

Type: Function.

Description:

  Gets whether the file is open.

Signature:

isOpen: function(self: File): boolean

Returns:

Return TypeDescription
booleanTrue if the file is currently open, false otherwise.

getSize

Type: Function.

Description:

  Returns the file size.

Signature:

getSize: function(self: File): number | nil, string | nil

Returns:

Return TypeDescription
number or nilThe file size in bytes.

read

Type: Function.

Description:

  Read a number of bytes from a file.

Signature:

read: function(self: File, size?: integer): string | nil, number | string

Parameters:

ParameterTypeDescription
sizeintegerThe number of bytes to read. (Default: all.)

Returns:

Return TypeDescription
string | nilcontents — The contents of the read bytes.
number | stringsize — How many bytes have been read.

read

Type: Function.

Description:

  Read a number of bytes from a file.

Signature:

read: function(self: File, container: string, size?: integer): string | nil, number | string

Parameters:

ParameterTypeDescription
containerstringWhat type to return the file's contents as.
sizeintegerThe number of bytes to read. (Default: all.)

Returns:

Return TypeDescription
string | nilFileData or string containing the read bytes.
number | stringsize — How many bytes have been read.

read

Type: Function.

Description:

  Read a number of bytes from a file.

Signature:

read: function(self: File, container: string, size?: integer): FileData | nil, number | string

Parameters:

ParameterTypeDescription
containerstringWhat type to return the file's contents as.
sizeintegerThe number of bytes to read. (Default: all.)

Returns:

Return TypeDescription
FileData | nilFileData or string containing the read bytes.
number | stringsize — How many bytes have been read.

write

Type: Function.

Description:

  Write data to a file.

Signature:

write: function(self: File, data: string, size?: integer): boolean | nil, string | nil

Parameters:

ParameterTypeDescription
datastringThe string data to write.
sizeintegerHow many bytes to write. (Default: all.)

Returns:

Return TypeDescription
boolean | nilsuccess — Whether the operation was successful.
string | nilerr — The error string if an error occurred.

write

Type: Function.

Description:

  Write data to a file.

Signature:

write: function(self: File, data: FileData, size?: integer): boolean | nil, string | nil

Parameters:

ParameterTypeDescription
dataFileDataThe Data object to write.
sizeintegerHow many bytes to write. (Default: all.)

Returns:

Return TypeDescription
boolean | nilsuccess — Whether the operation was successful.
string | nilerr — The error string if an error occurred.

flush

Type: Function.

Description:

  Flushes any buffered written data in the file to the disk.

Signature:

flush: function(self: File): boolean | nil, string | nil

Returns:

Return TypeDescription
boolean or nilWhether the file successfully flushed any buffered data to the disk.
string or nilThe error string, if an error occurred and the file could not be flushed.

isEOF

Type: Function.

Description:

  Gets whether end-of-file has been reached.

Signature:

isEOF: function(self: File): boolean

Returns:

Return TypeDescription
booleanWhether EOF has been reached.

tell

Type: Function.

Description:

  Returns the position in the file.

Signature:

tell: function(self: File): number | nil, string | nil

Returns:

Return TypeDescription
number or nilThe current position.

seek

Type: Function.

Description:

  Seek to a position in a file

Signature:

seek: function(self: File, position: number): boolean

Parameters:

ParameterTypeDescription
positionnumberThe position to seek to

Returns:

Return TypeDescription
booleanWhether the operation was successful

lines

Type: Function.

Description:

  Iterate over all the lines in a file.

Signature:

lines: function(self: File): function(): string | nil

Returns:

Return TypeDescription
function or string or nilThe iterator (can be used in for loops).

setBuffer

Type: Function.

Description:

  Sets the buffer mode for a file opened for writing or appending. Files with buffering enabled will not write data to the disk until the buffer size limit is reached, depending on the buffer mode. File:flush will force any buffered data to be written to the disk.

Signature:

setBuffer: function(self: File, mode: BufferMode, size?: integer): boolean

Parameters:

ParameterTypeDescription
modeBufferModeThe buffer mode to use.
sizeintegerThe maximum size in bytes of the file's buffer. (Default: 0.)

Returns:

Return TypeDescription
booleanWhether the buffer mode was successfully set.

getBuffer

Type: Function.

Description:

  Gets the buffer mode of a file.

Signature:

getBuffer: function(self: File): BufferMode, integer

Returns:

Return TypeDescription
BufferModeThe current buffer mode of the file.
integerThe maximum size in bytes of the file's buffer.

getMode

Type: Function.

Description:

  Gets the FileMode the file has been opened with.

Signature:

getMode: function(self: File): FileMode

Returns:

Return TypeDescription
FileModeThe mode this file has been opened with.

getFilename

Type: Function.

Description:

  Gets the filename that the File object was created with. If the file object originated from the love.filedropped callback, the filename will be the full platform-dependent file path.

Signature:

getFilename: function(self: File): string
getExtension: function(self: File): string

Returns:

Return TypeDescription
stringThe filename of the File.