Skip to Content
Skip to Content
ClassesFileHandle

FileHandle


A FileHandle is an object that represents an open file. It provides methods to read, write, and manipulate the file.

Methods

read

read from the file

:read(amount: number?): string :read(amount: number?, bytes: false?): string :read(amount: number?, bytes: true): buffer :read(amount: number?, bytes: boolean?): string | buffer

Parameters

  • amount: number? - The amount of bytes to read, if not provided, it will read until EOF or luau limit
  • bytes: false? | true | boolean? - If true, the function will return a buffer instead of a string.

Throws

  • IO Error

readSync

Read the contents of the file synchronously.

:readSync(amount: number?): string :readSync(amount: number?, bytes: false?): string :readSync(amount: number?, bytes: true): buffer :readSync(amount: number?, bytes: boolean?): string | buffer

Parameters

  • amount: number? - The amount of bytes to read, if not provided, it will read until EOF or luau limit
  • bytes: false? | true | boolean? - If true, the function will return a buffer instead of a string.

Throws

  • IO Error

write

Write the contents to the file.

:write(contents: string | buffer): ()

Parameters

  • contents: string | buffer - The contents to write to the file.

Throws

  • IO Error

writeSync

Write the contents to the file synchronously.

:writeSync(contents: string | buffer): ()

Parameters

  • contents: string | buffer - The contents to write to the file.

Throws

  • IO Error

append

Append the contents to the file.

:append(contents: string | buffer): ()

Parameters

  • contents: string | buffer - The contents to append to the file.

Throws

  • IO Error

appendSync

Append the contents to the file synchronously.

:appendSync(contents: string | buffer): ()

Parameters

  • contents: string | buffer - The contents to append to the file.

Throws

  • IO Error

getSize

Returns the size of the file in bytes.

:getSize(): number

Throws

  • IO Error

getSeekPosition

Returns the current seek position of the file.

:getSeekPosition(): number

Throws

  • IO Error

seekFromEnd

Seeks the file from the end.

:seekFromEnd(amount: number?): ()

Parameters

  • amount: number? - The amount of bytes to seek from the end of the file.
    • default: 0

Throws

  • IO Error

seekTo

Seeks the file to a specific position.

:seekTo(amount: number?): ()

Parameters

  • amount: number? - The amount of bytes to seek from the beginning of the file.
    • default: 0

Throws

  • IO Error

seekBy

Seeks the file by relative to the current position.

:seekBy(amount: number?): ()

Parameters

  • amount: number? - The amount of bytes to seek from the current seek.
    • default: 0

Throws

  • IO Error

lock

Locks the file for reading or writing.

:lock(lockMode: ("shared" | "exclusive" | "none")?): boolean

Parameters

  • lockMode: ("shared" | "exclusive" | "none")? - The mode to lock the file.
    • default: "exclusive"

Throws

  • IO Error

unlock

Unlocks from the current lock mode.

:unlock(): ()

Throws

  • IO Error

sync

Flushes the file. Writes the contents to the disk. This does not yield, it would block the process until the file is flushed.

:sync(): ()

Throws

  • IO Error

readonly

Get readonly state or set the file to readonly.

:readonly(): boolean :readonly(enabled: boolean): ()

Parameters

  • enabled: boolean? - Whether to set the file to readonly, if nil, returns the current state.

Throws

  • IO Error

close

Close the file handle.

:close(): ()

Throws

  • IO Error
Last updated on