File I/O
File operations for reading and writing files. The IO::File class provides both static utility methods and instance-based file operations.
xxml
#import Language::Core;
#import Language::IO;Static Methods
Static methods provide quick operations without needing to create a file instance.
exists
Check if a file exists.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| exists | path: String^ | Bool^ | True if file exists |
xxml
Instantiate Bool^ As <found> = IO::File::exists(String::Constructor("config.txt"));
If (found) -> {
Run Console::printLine(String::Constructor("File exists"));
}delete
Delete a file.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| delete | path: String^ | Bool^ | True if deleted |
copy
Copy a file to a new location.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| copy | srcPath: String^, dstPath: String^ | Bool^ | True if copied |
xxml
Instantiate Bool^ As <copied> = IO::File::copy(
String::Constructor("source.txt"),
String::Constructor("dest.txt")
);rename
Rename or move a file.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| rename | oldPath: String^, newPath: String^ | Bool^ | True if renamed |
sizeOf
Get file size in bytes.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| sizeOf | path: String^ | Integer^ | Size in bytes |
readAll
Read entire file content as a string.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| readAll | path: String^ | String^ | File content |
xxml
Instantiate String^ As <content> = IO::File::readAll(String::Constructor("readme.txt"));
Run Console::printLine(content);Instance Methods
Constructor
Open a file with a specific mode.
| Mode | Description |
|---|---|
"r" | Read (file must exist) |
"w" | Write (creates or truncates) |
"a" | Append (creates if needed) |
xxml
// Read mode
Instantiate IO::File^ As <inFile> = IO::File::Constructor(
String::Constructor("input.txt"),
String::Constructor("r")
);
// Write mode (creates/truncates)
Instantiate IO::File^ As <outFile> = IO::File::Constructor(
String::Constructor("output.txt"),
String::Constructor("w")
);
// Append mode
Instantiate IO::File^ As <logFile> = IO::File::Constructor(
String::Constructor("log.txt"),
String::Constructor("a")
);File State
| Method | Parameters | Returns | Description |
|---|---|---|---|
| close | — | None^ | Close file handle |
| isOpen | — | Bool^ | True if file is open |
| eof | — | Bool^ | True if at end of file |
| size | — | Integer^ | Size in bytes |
Reading
| Method | Parameters | Returns | Description |
|---|---|---|---|
| readLine | — | String^ | Read next line |
xxml
While (file.eof().not()) -> {
Instantiate String^ As <line> = file.readLine();
Run Console::printLine(line);
}Writing
| Method | Parameters | Returns | Description |
|---|---|---|---|
| writeString | text: String^ | Integer^ | Bytes written |
| writeLine | text: String^ | Integer^ | Bytes written (with newline) |
| flush | — | Integer^ | Flush buffered data to disk |
xxml
Run file.writeLine(String::Constructor("Line 1"));
Run file.writeLine(String::Constructor("Line 2"));
Run file.flush();Complete Example
file_io_example.xxml
1 #import Language::Core; 2 #import Language::IO; 3 #import Language::System; 4 5 [ Entrypoint 6 { 7 // Write a file 8 Instantiate IO::File^ As <out> = IO::File::Constructor( 9 String::Constructor("example.txt"), 10 String::Constructor("w") 11 ); 12 Run out.writeLine(String::Constructor("Hello, World!")); 13 Run out.writeLine(String::Constructor("This is XXML file I/O.")); 14 Run out.close(); 15 16 // Check file exists and size 17 If (IO::File::exists(String::Constructor("example.txt"))) -> { 18 Instantiate Integer^ As <size> = IO::File::sizeOf(String::Constructor("example.txt")); 19 Run Console::printLine(String::Constructor("File size: ").append(size.toString()).append(String::Constructor(" bytes"))); 20 } 21 22 // Read file line by line 23 Instantiate IO::File^ As <in> = IO::File::Constructor( 24 String::Constructor("example.txt"), 25 String::Constructor("r") 26 ); 27 Run Console::printLine(String::Constructor("Contents:")); 28 While (in.eof().not()) -> { 29 Instantiate String^ As <line> = in.readLine(); 30 Run Console::printLine(line); 31 } 32 Run in.close(); 33 34 // Read entire file at once 35 Instantiate String^ As <all> = IO::File::readAll(String::Constructor("example.txt")); 36 Run Console::printLine(String::Constructor("Full content:")); 37 Run Console::printLine(all); 38 39 // Cleanup 40 Run IO::File::delete(String::Constructor("example.txt")); 41 42 Exit(0); 43 } 44 ]