File System & I/O
The File class and FileMode enum provide everything you need to read and write files, as well as query the file system (checking existence, listing directory contents, parsing paths, etc.). You must import this module using @import("std/file").
Opening, Reading, and Writing
To interact with a file's contents, instantiate a File object using a path and a FileMode. Always remember to call close() when you are done to free up system resources!
For reading large files efficiently, you can use forEachLine by passing a named callback function.
@import("std/file")
// 1. Writing to a file
val outFile = File("notes.txt", FileMode.WRITE)
outFile.write("Hello Autolang!\n")
outFile.write("This is the second line.")
outFile.close()
// 2. Reading the entire file at once
val inFile = File("notes.txt", FileMode.READ)
val content = inFile.readText()
println("--- FULL TEXT ---")
println(content)
// 3. Reading line-by-line (using a function pointer callback)
func printLine(line: String) {
println("Read line: " + line)
}
println("--- LINE BY LINE ---")
inFile.seek(0) // Reset pointer to the beginning of the file
inFile.forEachLine(printLine)
inFile.close()File System Utilities
The File class includes many static methods that let you inspect paths and directories without actually opening the file. You can check if a file exists, get its size, parse its extension, or list all files in a directory.
@import("std/file")
val targetPath = "notes.txt"
// Check existence
if (File.exists(targetPath)) {
println("File name: ${File.getName(targetPath)}")
println("Extension: ${File.getExtension(targetPath)}")
println("Size: ${File.getSize(targetPath)} bytes")
// Deleting the file
val deleted = File.delete(targetPath)
println("Was deleted? ${if (deleted) "Yes" else "No"}")
} else {
println("File not found!")
}
// Working with directories
val dirPath = "src"
if (File.isDirectory(dirPath)) {
val allFiles = File.getAllFiles(dirPath)
println("Found ${allFiles.size()} files in '${dirPath}'")
}Reference
FileMode Enum
Used to specify the access mode when opening a file.
READ- Open for reading only.WRITE- Open for writing (creates a new file or truncates an existing one).APPEND- Open for appending (writes are added to the end of the file).READ_WRITE- Open for reading and writing.WRITE_READ- Open for reading and writing (truncates first).APPEND_READ- Open for reading and appending.
Instance Methods (I/O Operations)
File(path: String, mode: FileMode)- Opens a file with the specified mode.readText(): String- Reads the entire file content into a String.forEachLine(fn: (String) -> Void)- Reads the file line by line, executing the callback function for each line.write(text: String)- Writes the provided string to the file.seek(position: Int)- Moves the read/write pointer to a specific byte position.close()- Closes the file stream and releases system resources.
Static Methods (System Utilities)
File.exists(path: String): Bool- Returns true if the file or directory exists.File.delete(path: String): Bool- Deletes the file or directory.File.isDirectory(path: String): Bool- Returns true if the path points to a directory.File.isFile(path: String): Bool- Returns true if the path points to a regular file.File.getSize(path: String): Int- Returns the size of the file in bytes.File.getAllFiles(dirPath: String): Array<String>- Returns an array of file paths contained within the directory.File.getName(path: String): String- Extracts the file name (with extension) from the path.File.getExtension(path: String): String- Extracts the extension (e.g., "txt") from the path.File.getParent(path: String): String- Returns the parent directory path.File.getAbsolutePath(path: String): String- Resolves the absolute system path.File.getLastModified(path: String): Int- Returns the Unix timestamp (since 1970) of when the file was last modified.
