File I/O
Overview
The File module provides functions for reading, writing, and managing files on disk.
Convenience Methods
Read or write an entire file in one call:
string text = File.ReadAllText("data.txt");int8[] bytes = File.ReadAllBytes("image.png");string[] lines = File.ReadAllLines("data.txt");
File.WriteAllText("output.txt", "Hello, file!\n");File.WriteAllBytes("header.bin", bytes);File.AppendAllText("log.txt", "New entry\n");Temporary Files
File temp = File.CreateTemp();temp.WriteString("temporary data");Console.WriteLine(temp.Path);temp.Close();Stream-Based I/O
For large files, use File.Open with instance methods:
File file = File.Open("large.txt", "r");while (file.IsOpen){ string line = file.ReadLine(); if (line == "") { break; } Console.WriteLine(line);}file.Close();Instance Methods
| Method | Description |
|---|---|
file.Read(count) | Read count bytes |
file.ReadLine() | Read one line |
file.Write(bytes) | Write raw bytes |
file.WriteString(s) | Write a string |
file.WriteLine(s) | Write a string with newline |
file.Seek(pos) | Seek to byte position |
file.Flush() | Flush buffered writes |
file.Close() | Close the file |
Instance Properties
| Property | Type | Description |
|---|---|---|
file.Position | int | Current byte offset |
file.Length | int | Total file size |
file.CanRead | bool | Opened for reading |
file.CanWrite | bool | Opened for writing |
file.IsOpen | bool | File is still open |
file.Path | string | File path |
File Locking
Lock files to prevent concurrent access:
File f = File.Open("data.bin", "rw");f.Lock(); // exclusive lock (entire file)// ... write safely ...f.Unlock();
f.LockShared(); // shared/read lock// ... read safely ...f.Unlock();
f.LockRange(0, 1024); // lock a byte rangef.Unlock();f.Close();| Method | Description |
|---|---|
file.Lock() | Exclusive lock on the entire file |
file.LockRange(offset, length) | Lock a specific byte range |
file.LockShared() | Shared (read) lock |
file.Unlock() | Release the lock |
The file.IsLocked property indicates whether the file is currently locked.
File Operations
File.Exists("config.json");File.Delete("temp.txt");File.Copy("source.txt", "dest.txt");File.Move("old.txt", "new.txt");Directory Operations
Directory.Exists("./build");Directory.Create("./output");Directory.CreateAll("./a/b/c");Directory.Delete("./temp");
string[] files = Directory.GetFiles("./data");string[] dirs = Directory.GetDirectories("./src");Path Utilities
string full = Path.Combine("./dir", "file.txt");string name = Path.GetFileName("./dir/file.txt"); // "file.txt"string dir = Path.GetDirectory("./dir/file.txt"); // "./dir"string ext = Path.GetExtension("report.pdf"); // ".pdf"string abs = Path.GetFullPath("./relative"); // absolute pathFileWatcher
Monitor the file system for changes:
FileWatcher watcher = FileWatcher.Create("./data", FileChangeType.All);watcher.Start();
// Non-blocking checkbool changed = watcher.PollChange();
// Blocking waitwatcher.WaitForChange();
// Wait with timeout (milliseconds)watcher.WaitForChangeTimeout(5000);
watcher.Stop();| Method | Description |
|---|---|
FileWatcher.Create(path, filter) | Create a watcher with a change type filter |
watcher.Start() | Begin watching |
watcher.Stop() | Stop watching |
watcher.PollChange() | Non-blocking check for changes |
watcher.WaitForChange() | Block until a change occurs |
watcher.WaitForChangeTimeout(ms) | Block with a timeout |
| Property | Type | Description |
|---|---|---|
watcher.Path | string | Watched path |
watcher.Filter | int | FileChangeType filter |
watcher.IsWatching | bool | Whether currently active |