Skip to main content

TextFile

Represents a text file handle.

Use this class to read and write plain text files.

Example

var f = TextFile("notes.txt", "w")
f.write("hello\n")
f.append("world\n")
var lines = TextFile("notes.txt").read_lines()

Constructors

TextFile(const string path) -> TextFile

Opens a text file in read mode.

Parameters

  • path: File path.

Example

var f = TextFile("notes.txt")

TextFile(const string path, const string mode) -> TextFile

Opens a text file with an explicit mode.

Modes:

  • "r": read-only, file must exist
  • "r+": read/write, file must exist
  • "w": read/write, create or overwrite
  • "w-" or "x": read/write, create, fail if it exists
  • "a": read/write, create if missing

Parameters

  • path: File path.
  • mode: File open mode.

Example

var f = TextFile("notes.txt", "w")

Members

NameDescription
appendAppends text to the end of the file.
pathReturns the resolved file path.
readReads the entire file as text.
read_linesReads the file as a list of lines (line endings stripped).
writeWrites text to the file, truncating first.
write_linesWrites a list of lines to the file, truncating first.